1

So I was making a simple rock paper scissors game, and I wanted to put it as a function. I also wanted it to loop 10 times before deciding if the user won or lost. But the code just doesn't work and keeps giving random errors.

#code for rock paper scissors
import random 

l = ["rock","paper","scissors"]

def rps():
    q = 0
    wincount = 0
    while q < 10:
        w = random.choice(l)
        x = input("Choose rock paper or scissors all lowercase: ") 
        if w == "rock":
            if x == "rock":
                print("It's a draw")
            if x == "paper":
                print("You lost :(")
            if x == "scissors":
                print("You win!")
                wincount += 1
        elif w == "paper":
            if x == "rock":
                print("You lost :(")
            if x == "paper":
                print("It's a draw")
            if x == "scissors":
                print("You win!") 
                wincount += 1
        elif w == "scissors":
            if x == "rock":
                print("You win!")
                wincount += 1
            if x == "paper":
                print("You lost :(")
            if x == "scissors":
                print("It's a draw")
        q += 1
    if wincount >= 5:
        print("You won the game!")    

The error is

PS D:\project percy> & C:/Users/Dell/AppData/Local/Programs/Python/Python39/python.exe "d:/project percy/cogs/asiufysduifhi.py"
PS D:\project percy> rps()
At line:1 char:5
+ rps()
+     ~
An expression was expected after '('.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordE  
   xception
    + FullyQualifiedErrorId : ExpectedExpression```
2
  • What IDE are you using, this doesn't look like a standard Python error message. Commented Jul 22, 2021 at 14:40
  • 1
    you're trying to run python code directly in the terminal, that second line should be within the code or the first file imported to a python shell Commented Jul 22, 2021 at 14:41

3 Answers 3

1

The error seems not because of python. It should be because of Powershell.

PowerShell needs the ampersand to interpret the string as a filename.

As you are already using ampersand so not sure but try to run your command within proper quotes.

Like

& "C:/Users/Dell/AppData/Local/Programs/Python/Python39/python.exe" "d:/project percy/cogs/asiufysduifhi.py"

Or if above wont works then give a try to this:

& "C:/Users/Dell/AppData/Local/Programs/Python/Python39/python.exe" d:/project percy/cogs/asiufysduifhi.py
Sign up to request clarification or add additional context in comments.

Comments

0

As others have said already, he error does not come from python but rather from powershell. I don't have windows to test but I think the problem is that you are trying to call rps() which is a python function, from the powershell prompt:

PS D:\project percy> & C:/Users/Dell/AppData/Local/Programs/Python/Python39/python.exe "d:/project percy/cogs/asiufysduifhi.py"

Call your python file that defines the function BUT NEVER CALLS IT

PS D:\project percy> rps()

Try to execute a powershell command named rps, which might exist but should be called differently.

Proposed solution

Try to add rps() at the end of your python file:

def rps():
    .....
    if wincount >= 5:
        print("You won the game!")

rps()

Comments

0

The problem is you forgot to pass the list to the function as well as calling the function.

try this:

# code for rock paper scissors
import random

l = ["rock", "paper", "scissors"]


def rps(l):
    q = 1
    wincount = 0
    while q < 11:
        w = random.choice(l)
        print()
        print(q, "time")
        x = input(f" Choose rock paper or scissors all lowercase: ")
        if w == "rock":
            if x == "rock":
                print("It's a draw")
            if x == "paper":
                print("You lost :")
            if x == "scissors":
                print("You win!")
                wincount += 1
        elif w == "paper":
            if x == "rock":
                print("You lost :")
            if x == "paper":
                print("It's a draw")
            if x == "scissors":
                print("You win!")
                wincount += 1
        elif w == "scissors":
            if x == "rock":
                print("You win!")
                wincount += 1
            if x == "paper":
                print("You lost :")
            if x == "scissors":
                print("It's a draw")
        q += 1
    if wincount >= 5:
        print("You won the game!")

calling = rps(l)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.