
modulo random no funciona
Publicado por Carolina (9 intervenciones) el 22/08/2015 20:31:10
Hola a todos:
Estoy trabajando en el sgte ejercicio:
"Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. Extras: Keep the game going until the user types “exit” and keep track of how many guesses the user has taken, and when the game ends, print this out."
Bueno pues este es mi código. Funciona todo muy bien excepto por la función random. Por algún motivo siempre me imprime el mismo numero. Se supone que cada vez debería imprimir un numero diferente entre el 1 y el 9, pero por alguna razón, en mi código siempre se imprime el mismo. Alguien me podría explicar por que? Funcionaba perfectamente antes de introducir el while. Creo que tiene que ver con eso, pero no estoy segura.
Muchas gracias!.
Mi código:
Por cierto, esta es la solución del ejercicio. Pero yo quiero saber simplemente que esta mal con mi código:
Estoy trabajando en el sgte ejercicio:
"Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. Extras: Keep the game going until the user types “exit” and keep track of how many guesses the user has taken, and when the game ends, print this out."
Bueno pues este es mi código. Funciona todo muy bien excepto por la función random. Por algún motivo siempre me imprime el mismo numero. Se supone que cada vez debería imprimir un numero diferente entre el 1 y el 9, pero por alguna razón, en mi código siempre se imprime el mismo. Alguien me podría explicar por que? Funcionaba perfectamente antes de introducir el while. Creo que tiene que ver con eso, pero no estoy segura.
Muchas gracias!.
Mi código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import random
num = random.randint(1,9)
u = ""
count = 0
while u != "exit":
u = input("Adivine el numero: ")
if u == "exit":
break
u = int(u)
count += 1
if u < num:
print("Too low! -->", num)
elif u > num:
print("Too high! -->", num)
else:
print("Exactly right! -->", num)
print("Y solo te tomo ",count," veces")
Por cierto, esta es la solución del ejercicio. Pero yo quiero saber simplemente que esta mal con mi código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import random
number = random.randint(1,9)
guess = 0
count = 0
while guess != number and guess != "exit":
guess = input("What's your guess?")
if guess == "exit":
break
guess = int(guess)
count += 1
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
else:
print("You got it!")
print("And it only took you",count,"tries!")
Valora esta pregunta


0