[Tutor] assigning a variable a value
Kyle Benak
kbenak86 at yahoo.com
Thu May 5 02:19:57 CEST 2011
Hi,
I am learning python and I am trying to write a simple "guess the number"
game. I wrote the program in the IDLE, and I set the variable tries=1 to keep up
with the number of tries it takes to guess the number, but when I try to run the
program it gives the error message "improper syntax" and highlights the word
tries. So, I assigned the variable tries the value 1 in the python shell window
and it works fine there. Can you tell me why it won't work in the program? A
copy of my code is below for clarification.
#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 correct
import random
print('Welcome to "Guess My Number"!')
print('\nI\'m thinking of a number between 1 and 100.')
print('Try and guess the number in as few turns as possible.')
#set initial values
num = random.randint(1,100)
guess = int(input('Take a guess: ')
tries = 1
while guess != num:
if guess > num:
print('Too high. Guess lower.')
else:
print('Too low. Guess higher.')
guess=input('Take another guess: ')
tries += 1
print('You guessed it. The number was',num)
print('Congratulations, You guessed the correct answer.'
print('It only took you',tries,'tries.')
input('\n\nPress enter to exit.')
I highlighted the problem in red.
Thanks for any help.
Kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110504/3d25cf75/attachment-0001.html>
More information about the Tutor
mailing list