Rock paper scissors game won't print computer choice right

Sorry if this is a stupid easy fix, I am VERY new to coding in general
So I am trying to code a rock paper scissors game but my result isn’t coming out right and idk why

My Code:

import random

while True:
        user_action = input("Enter a choice (rock, paper, scissors): ")
        possible_actions = ["rock", "paper", "scissors"]
        computer_action = random.choice(possible_actions)
        print("computer chose {computer_action}.")

and then it responds with this when I run

computer chose {computer_action}.

and it should fill in {computer_action} with a random choice but its not and idk why

You want to use an f-string: print(f"computer chose {computer_action}.") (note the f right before the first ")

1 Like

Now its giving me a syntax error? “File “RPS.py”, line 7 SyntaxError: invalid syntax”

I would assume you made a syntax error when making that change, I have no idea what it is because I can’t see the code.

import random

while True:
        user_action = input("Enter a choice (rock, paper, scissors): ")
        possible_actions = ["rock", "paper", "scissors"]
        computer_action = random.choice(possible_actions)
        print(f"computer chose {computer_action}.")

seems exactly the same to me

Indeed, that code doesn’t have any syntax problems, but maybe it’s not exactly the code that you tried to run.

Usually a syntax error will provide some more context about exactly what was wrong (particularly in newer versions of Python)

Thanks for your help though! I’ll try to look more into it but its not really giving me anything

Well, yes; but if the problem is an old version of Python that doesn’t support this feature, then a generic message would very much be expected. (And even the most recent versions still have SyntaxErrors that say only invalid syntax most of the time; they just have better highlighting.)

OP: you need to be using at least version 3.6 for this feature. You can check what version of Python is running the script, from within the script, like so:

import sys

print(sys.version)

If you have an old version of Python it is strongly recommended to update (do not try to remove any version of Python that was already on your computer when you got it). The oldest version that is still supported is 3.8. For details:

1 Like

In the future, please show complete error messages by copying and pasting the entire stack trace (i.e., starting from the line that says Traceback (most recent call last): until the end), and format them the same way as the code (they’re designed to look right in a terminal).

The only error I can think of is that you are using an earlier (very old!) version of python. You need at least python 3.6 to use f-strings.

For pre-3.6, change line 7 to this:

print("computer chose {}.".format(computer_action))

But if you can, you should update python.

1 Like

English can be such a treacherous language:

The “F” should be placed to the left of the string.

Given that English is read from left-to-right, the word “before” is also an anathema. Even the manual says " begin a string with f or F before the opening quotation mark or triple quotation mark". Compared with (say): “A formatted-string is indicated with an f or F character-prefix, followed by a string-literal delimited by quotation-marks in the usual fashion”.

Either way, (hah! there’s only the computer’s way), the OP should review ## 7.1. Fancier Output Formatting

It didn’t even occur to me that this was such an old version of Python…I guess maybe 3.6 or older is pre-installed on some systems?

Doesn’t happen for syntax errors, though.

Fair point. But it will still show every other line as usual, including the actual traceback with the corresponding line of code, all with the appropriate whitespace to look right when formatted as multi-line code.