Re: [Tutor] Help with guess my number game

2014-10-13 Thread Alan Gauld

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with guess my number game

2014-10-13 Thread Danny Yoo
>
> if guess != the_number:
>
> print ("you failed, the number was", the_number)
>
> elif guess==the_number:
>
> print("You guessed it!  The number was", the_number)
>
> print("And it only took you", tries, "tries!\n")


This block of code appears to be applied for every iteration through
your loop.  Is that your intention?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with guess my number game

2014-10-13 Thread אופיר לירון
Hi,I am new into Python, and using the bookPython Programming for the Absolute Beginner by Michael Dawson.One of the taks in chapte 3 is to change the "guess my number game" to include only 5 gusses and give appropriate messege at the end (in case no sucssesful guess was done).I have tried to add break for the loop after the variable "tries>5". however it seems that the program gets out from the loop after only 2 gusses. could someone give the reson or the solution to this issue.The cose is attached below:# Guess My Number## The computer picks a random number between 1 and 100# The player tries to guess it and the computer lets# the player know if the guess is too high, too low# or right on the moneyimport random  print("\tWelcome to 'Guess My Number'!")print("\nI'm thinking of a number between 1 and 100.")print("Try to guess it in as few attempts as possible.\n")# set the initial valuesthe_number = random.randint(1, 100)guess = int(input("Take a guess: "))tries = 1# guessing loopwhile guess != the_number:    if guess > the_number:        print("Lower...")    else:        print("Higher...")                guess = int(input("Take a guess: "))    tries += 1    if tries > 5:        break              if guess != the_number:        print ("you failed, the number was", the_number)    elif guess==the_number:        print("You guessed it!  The number was", the_number)        print("And it only took you", tries, "tries!\n")input("\n\nPress the enter key to exit.")Thanks for your helpofi...@walla.co.ilWalla! Mail - Get your free unlimited mail today___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor