First python program, syntax error in while loop
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
Okay, thank you very much for the timely replies, heres what i have now: title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random number = random.randrange(99) + 1 tries = 0 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: number = int(raw_input("Sorry, my number is higher than that! \n Try again:")) tries += 1 print "good job, you won!" raw_input("\n\n Press any key to exit..") ## it seems to stick with "higher" or "lower" after my first guess, whichever it ## is -- http://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
Oh wow I can't believed I derped that hard Thanks Lol. -- http://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails.. title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random number = random.randrange(99) + 1 tries = 0 guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) while (guess != number): if (guess > number): guess = int(raw_input("Sorry, my number is lower than that! \n Try again:")) tries += 1 if (guess < number): guess = int(raw_input("Sorry, my number is higher than that! \n Try again:")) tries += 1 if (tries > 5): print "Sorry, you lost!" break print "\nCongratulations! You guessed my number in", tries, "tries" raw_input("\n\n Press any key to exit..") ## this results in the congratulations printing after the second/third guess and ## continuing -- http://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
Thank you! It's 100% functional now, here's the final project: title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random number = random.randrange(99) + 1 tries = 0 guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) while (guess != number) and (tries <5): if (guess > number): guess = int(raw_input("Sorry, my number is lower than that! \n Try again:")) tries += 1 if (guess < number): guess = int(raw_input("Sorry, my number is higher than that! \n Try again:")) tries += 1 if (tries <5): print "\nCongratulations! You guessed my number in", tries, "tries" else: print "\nSorry, you took too many tries to guess my number!" raw_input("\n\n Press any key to exit..") ## Maybe now I can work on a useful project -- http://mail.python.org/mailman/listinfo/python-list