简体   繁体   中英

Syntax error on the colon in an if statement

I am new to python, and am making a sort-of game as one of my first projects that guesses a number between 1 and 10, then the user guesses it. They have three guesses, and the program tells the user if they need to go higher or lower on their next guess. The part of the code with the error in isn't crucial, as it only makes a guess not be wasted if the user puts in the same answer twice, allowing them to redo their guess the first time but not allowing a re-take the second. On the code, I have marked where the problem is. Like I said, I am really new to python and this is probably some amateur noobie mistake. Thanks in advance.

import time # This imports the time module.
import random # This imports the random module.

MyNumber = random.randrange(1,10) # This picks a number for the variable 'MyNumber'

firstGuess = int(input('Ok then, we shall begin! What is your first guess?'))
print()
if firstGuess == (MyNumber):
 print('Well done! You win!')
 time.sleep(3)
 exit()
if firstGuess < MyNumber:
 print('Go Higher!')
 time.sleep(1)
if firstGuess > MyNumber:
 print('Go Lower!')
 time.sleep(1)

print()
secondGuess = int(input('Better luck this time! What is your second guess?'))
print()
if secondGuess == firstGuess:
 print('You tried that one last time! Don\'t worry, I won\'t count that one!')
 bungled = (1)
 secondGuess = int(input('What is your second guess?')
 if secondGuess == firstGuess:  # This colon is causing the problem. <===========
  print('You\'ve already tried that one twice!')
  bungled = (2)
if secondGuess == MyNumber:
 print('Well done! You win!')
 time.sleep(3)
 exit()
if secondGuess < MyNumber:
 print('Go Higher!')
 time.sleep(1)
if secondGuess > MyNumber:
 print('Go Lower!')
 time.sleep(1)

print()
thirdGuess = int(input('This is your final chance! What is your third guess?'))
print()
if thirdGuess == MyNumber:
 print('Well done! You win!')
 time.sleep(3)
 exit()
if thirdGuess < MyNumber:
 MyNumber = str(MyNumber)
 print('Sorry! You lost! The number was '+MyNumber)
 time.sleep(1)
 exit()
if thirdGuess > MyNumber:
 MyNumber = str(MyNumber)
 print('Sorry! You lost! The number was '+MyNumber)
 time.sleep(1)
 exit()

It's not actually the colon. It's the unclosed bracket on the previous line.

When you get a weird SyntaxError , check for bracket balance before it.

The line above is missing a parenthesis. Change

secondGuess = int(input('What is your second guess?')

to

secondGuess = int(input('What is your second guess?'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM