First python program, syntax error in while loop

MRAB python at mrabarnett.plus.com
Fri May 3 13:36:52 EDT 2013


On 03/05/2013 18:18, ryankoch38 at gmail.com wrote:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
>
> import random
>
> tries = 0
> number = random.randrange(99) + 1
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
>
> while (guess != number):
> if (guess > number):
>      number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
>     tries += 1
> else if (guess < number):
>      number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
>      tries += 1
> print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
>
> raw_input("\n\n Press any key to exit..")
>
> ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P
>
Indentation in important in Python.

Also, "else if" should be "elif", and you ask for a guess only once.


title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")

import random

tries = 0
number = random.randrange(99) + 1

# Ask for first guess.
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")

while guess != number:
     if guess > number:
         number = int(raw_input("Sorry, my number is lower than that! \n 
Try again:")
         tries += 1
     elif guess < number:
         number = int(raw_input("Sorry, my number is higher than that! 
\n Try again:")
         tries += 1

     # Ask for next guess.
     guess = int(raw_input("Guess my number! Secret - It is between 1 
and 100 :")

print "Congratulations, you guessed my number! \n And it only took you" 
tries "tries!"

raw_input("\n\n Press any key to exit..")





More information about the Python-list mailing list