MCADAMS Posted August 13, 2012 Author Share Posted August 13, 2012 I'm learning programming for the first time with Python, trying to make a random number game. I have this:```#Self-written 'Guess the Number' gameimport random guessesTaken = 0print('Hello! What is your name?')myName = input()number = random.randint(1, 20)print('Ok, ' + myName + '. I am thinking of a number between 1 and 20\. Guess.')while guessesTaken < 6: print('Take a guess.') guess = input() guess = int(guess) guessesTaken = guessesTaken + 1 if guess < number: print('Too low!') if guess > number: print('Too high!') if guess == number: breakif guess == number: guessesTaken = str(guessesTaken) print('Congratulations! You guessed the number in ' + guessesTaken + ' guesses!'if guess != number: number = str(number) print('Sorry, the number we were looking for was ' + number)```When I run it I get "invalid syntax" and it points to the colon after```if guess != number:```I've double checked the code I'm referencing and they're identical it seems. Any help? Link to comment Share on other sites More sharing options...
Jumbofile Posted August 13, 2012 Share Posted August 13, 2012 Try getting rid of the !. Link to comment Share on other sites More sharing options...
cheatking Posted August 13, 2012 Share Posted August 13, 2012 Guess isn't available outside of the while loop.http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/ Link to comment Share on other sites More sharing options...
MCADAMS Posted August 13, 2012 Author Share Posted August 13, 2012 @MrSean:> Guess isn't available outside of the while loop.That was it. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now