[Tutor] Simple guessing game - need help with the math

Sibylle Koczian nulla.epistola at web.de
Fri Aug 15 09:37:14 CEST 2014


Am 13.08.2014 01:25, schrieb Greg Markham:
> while answer == "h" or "l" or "c":
>      print ("My guess is: ", guess, "\n")
>      answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
>      answer = answer.lower()
>      if answer == "h":
>          guess = round(int(guess + (change/2)))
>          change = change/2
>          tries += 1
>      elif answer == "l":
>          guess = round(int(guess - (guess/2)))
>          tries += 1
>      elif answer == "c":
>          print ("/n/nYay!  I win.  Shall we play again?\n\n")
>          os.system('cls' if os.name <http://os.name> == 'nt' else 'clear')
>      else:
>          print ("Invalid response.  Please try again.\n")
>

Something else is wrong, besides the math: this is an infinite loop, 
because (answer == "h" or "l" or "c") always evaluates to True.

And with the condition you probably wanted:

while answer == "h" or answer == "l" or answer == "c":

the loop wouldn't even start because you set answer = "" in the beginning.







More information about the Tutor mailing list