I was making a minigame and while I was making the over function to end the program syntax error shows up
The messed up but working code:
def over(lose=True, timeout=False):
print(‘GAME OVER!’)
if lose:
(space)print(f’Player {playrnd} lose!‘)
(space)print(’(took too long to answer)’ if timeout else ‘(Wrong response)’)
else:
(space)print(“It is a tie! (Ran out of numbers’ quota)”)
print(‘Run the program again to restart’)
quit()
The error code:
def over(lose=True, timeout=False):
print(‘GAME OVER!\n’+(f’Player {playrnd} lose! ({‘Took too long to answer’if timeout else ‘Wrong response’})‘if lose else ‘It is a tie!(Ran out of numbers that are set at the beginning)’)+’\nRun the program again to start over’)
quit()
(Due to some display error which deletes the spaces I will use (space) instead)
The variable ‘playrnd’ is an integer but it is outside the highlighted ‘invalid syntax’ area.
They work just the same, but for some reason one of them just go error
Here is the link to the project that shows this error:
I’ve had a look at the code and run the game a few times. The only error message I can get to appear related to the GAME OVER section of the code is shown below:
Is this the error you see? If not can you provide screenshots of the error message.
I’ve had a look at the above code. The issue is from
({'Took too long to answer...
It is because you are using the single quote for the f-string and then for the options within it. It doesn’t work that way.
You should try this
print('GAME OVER!\n'+(f'Player {playrnd} lose! ({"Took too long to answer" if timeout else "Wrong response"})' if lose else "It is a tie!(Ran out of numbers that are set at the beginning)")+'\nRun the program again to start over')