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()
They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.
if. Note the form of the 'if' statement: the keyword if, followed by a condition (e.g., x<6) followed by a colon.
In Python, a colon is required at the beginning of every block of code. It is easier to explain with an example. Notice how at the end of the if statement I have a colon. This tells Python that the next line of indented code should only be run IF the condition is true.
The message reads SyntaxError: invalid syntax , but that's not very helpful. The traceback points to the first place where Python could detect that something was wrong. To fix this sort of error, make sure that all of your Python keywords are spelled correctly.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With