(Python) f string shows syntax error while separated print() of same thing works fine

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)’)
(space)print(“It is a tie! (Ran out of numbers’ quota)”)
print(‘Run the program again to restart’)

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’)

(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:


Plz help

Hi @Taokyle thank you for your message.

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.

The error code
Since I have said it shows a syntax error(invalid syntax) I cannot even use try function.
Have no idea what is going on


It just show invalid syntax(pyflakes)

Oh I understand now. The Repl contains the working code, but you want to change it to the above?

So you mean this code is working, only not in code editor’s console?

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')
1 Like

No I meant that the Repl you shared earlier worked, but using the separated print commands.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.