python 3.x - Input an answer until a certain value is reached -


i attempting let user guess random number created program. if gets number wrong program tell him if guess higher or lower random number. ask him input again. program should loop until random number guessed correctly.

from random import randint random = (randint(0, 20)) guess = int(input("guess number: "), 0)  if guess > random:     print ("guess lower") elif guess < random:     print ("guess higher") else:     print("you guessed right!") 

i suggest learning for loops, while loops, , recursive function calls; can job done you.

here's simple example while loop (perform task, while something true):

from random import randint random = (randint(0, 20))  guess = none while guess != random:     guess = int(input("guess number: "), 0)      if guess > random:         print ("guess lower")     elif guess < random:         print ("guess higher")     else:         print("you guessed right!") 

until guess equal random, while loop keep asking input; once you've guessed correctly guess != random no longer true, , loop stop running.

note: i'm initializing variable guess none begin with, because none never equal integer value. don't intialize guess false because if random integer 0, python evaluates false == 0 true , loop never run.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -