[Tutor] Help with guess my number game

Alan Gauld alan.gauld at btinternet.com
Tue Oct 14 01:39:00 CEST 2014


On 13/10/14 11:40, אופיר לירון wrote:

> # set the initial values
>
> the_number = random.randint(1, 100)
> guess = int(input("Take a guess: "))
> tries = 1
>
> # guessing loop
> while guess != the_number:
>      if guess > the_number:
>          print("Lower...")
>      else:
>          print("Higher...")
>      guess = int(input("Take a guess: "))
>
>      tries += 1
>      if tries > 5:
>          break

so far so good....
almost...
>
>      if guess != the_number:
>          print ("you failed, the number was", the_number)

This is still inside the loop. You want to remove the
indentation so this only happens after you exit the loop.
Otherwise you tell the user the answer before they guess
it (or have 5 goes) and it doesn't work right if the
first guess is correct...

> input("\n\nPress the enter key to exit.")

You need the if/else to look like this.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list