Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, time
- class GranNError(Exception):
- def __init__(self,arg):
- self.args=arg
- cad ="\n Bienvenido al juego de Muertos y heridos\n"
- cad+=" Tienes que adivinar un numero entre 0000 y 9999.\n"
- cad+=" Prueba numeros y como pista recibiras 1 herido por cada cifra acertada.\n"
- cad+=" Si aciertas tambien su posicion, en vez de un herido, recibiras 1 muerto.\n"
- cad+=" Ganas cuando consigas 4 muertos, es decir, cuando ordenes los 4 numeros.\n"
- print(cad)
- n1=random.randrange(10)
- n2=random.randrange(10)
- n3=random.randrange(10)
- n4=random.randrange(10)
- n=n1*1000+n2*100+n3*10+n4
- while True:
- try:
- g=int(input(" Adivina que numero estoy pensando (0000-9999): "))
- if g>9999: raise GranNError("")
- break
- except ValueError: print(" Eso no es un numero, vuelve a intentarlo")
- except GranNError as e: print(" Numero no valido, prueba solo con 4 cifras")
- intentos = 0
- correcto=False
- dif = time.time()
- while not correcto :
- heridos=0
- muertos=0
- intentos+=1
- g1=int(g/1000)
- if g1==n1 : muertos+=1
- elif g1==n2 or g1==n3 or g1==n4 : heridos+=1
- g2=int(g/100-g1*10)
- if g2==n2 : muertos+=1
- elif g2==n1 or g2==n3 or g2==n4 : heridos+=1
- g3=int(g/10-g1*100-g2*10)
- if g3==n3 : muertos+=1
- elif g3==n1 or g3==n2 or g3==n4 : heridos+=1
- g4=int(g-g1*1000-g2*100-g3*10)
- if g4==n4 : muertos+=1
- elif g4==n1 or g4==n2 or g3==n3 : heridos+=1
- if muertos==4 : correcto=True
- else :
- cad = str(g1)+str(g2)+str(g3)+str(g4)
- while True:
- try:
- g=int(input(" En "+cad+" hay "+str(muertos)+" muertos y "+str(heridos)+" heridos. Sigue intentandolo: "))
- if g>9999: raise GranNError("")
- break
- except ValueError: print(" Eso no es un numero, vuelve a intentarlo")
- except GranNError as e: print(" Numero no valido, prueba solo con 4 cifras")
- dif = time.time()-dif
- puntuacion = int((1.0/intentos + 1.0/(dif+10**10))*1000)
- print(" Has acertado. Intentos: "+str(intentos)+". Tiempo: "+str(round(dif,3))+" segundos. Puntos: "+str(puntuacion))
- input(" Pulsa INTRO para salir")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement