[Tutor] assigning a variable a value
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___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] assigning a variable a value
On Thu, May 5, 2011 at 2:19 AM, Kyle Benak kbena...@yahoo.com wrote: 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. If the parser tells you there's a syntax error, it is often in the line it shows, but also often in the line preceding that. In this case, it is the latter: You have two opening brackets there, but only one closing bracket. -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] assigning a variable a value
Kyle Benak kbena...@yahoo.com wrote 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. First please post the actual error in full, not just a rough summary. Pythopn error messages are very informative and helpful once you know how to read them. However, in this case its not needed. The thing to remember with errors, especially syntax errors, is that Python reports them where it identifies the problem. But that may be a line or so later than where the actual error occurs. That is what happened here. Look at the guess= line and count the parentheses num = random.randint(1,100) guess = int(input('Take a guess: ') tries = 1 print('Too low. Guess higher.') guess=input('Take another guess: ') you probably need to convert to int() here too... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor