【发布时间】:2022-02-04 21:03:21
【问题描述】:
所以我在做一个简单的石头剪刀布游戏,我想把它做成一个函数。在决定用户是赢还是输之前,我还希望它循环 10 次。但是代码不起作用,并且不断给出随机错误。
#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!")
错误是
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```
【问题讨论】:
-
您使用的是什么 IDE,这看起来不像是标准 Python 错误消息。
-
你试图直接在终端中运行 python 代码,第二行应该在代码中或导入到 python shell 的第一个文件中
标签: python function while-loop