简体   繁体   中英

While-loop guessing number

Create an input field "Guess my age". If the age is correct print 'you are correct', if the age is lower than your age print 'I am older than that', if the entered age is higher than your age print ' I am not THAT old !!!' Use a while loop to ask for your age until the correct age is entered.

Here is my code so far:

answer = input('Guess my age')
   while answer != "22":
      if answer > "22":
         print("I am not THAT old!!")
         input ('Guess my age')
      if answer < "22":
         print("I am older than that")
         input('Guess my age')
   print("You are correct!")

If I enter "21" in the input box, for example, it will return saying "I am older than that" which is correct, but when it prompts for another number to be entered, say I put "45" it will continue to say "I am older than that", which is wrong and should say "I am not THAT old!!", and vice versa

What am I doing wrong?

You are not updating answer, and you are not making answer answer an int here:

answer = int(input('Guess my age'))
   while answer != 22:
      if answer > 22:
         print("I am not THAT old!!")
         answer = int(input ('Guess my age'))
      if answer < 22:
         print("I am older than that")
         answer = int(input('Guess my age'))
   print("You are correct!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM