Python 巨蟒石剪刀错误
我在尝试运行此程序时遇到了一个错误,即没有定义x。我是python新手。任何帮助都将不胜感激。另外,任何让这段代码更干净的方法都会很好Python 巨蟒石剪刀错误,python,Python,我在尝试运行此程序时遇到了一个错误,即没有定义x。我是python新手。任何帮助都将不胜感激。另外,任何让这段代码更干净的方法都会很好 变量playachoice是函数playainput()的本地变量。因此在playanput()的末尾添加: return playerinput 然后将x赋值更改为: x = playainput() 更新: 您有几个小错误,让我们试试: import random choices = ["rock", 'paper', 'scissors'] de
变量
playachoice
是函数playainput()
的本地变量。因此在playanput()的末尾添加:
return playerinput
然后将x赋值更改为:
x = playainput()
更新:
您有几个小错误,让我们试试:
import random
choices = ["rock", 'paper', 'scissors']
def playainput():
while True:
playerinput = input("Input rock, paper, or scissors: ")
if playerinput in choices:
print("You chose", playerinput)
break
print ("oops type again")
return playerinput
def choose(x):
choice = random.choice(x)
print ("I chose %s" % choice)
return choice
x = playainput()
y = choose(choices)
outcomes = {
("rock", "rock"): "tie!",
("rock", "paper"): "you lose",
("rock", "scissors"): "you win",
("paper", "rock"): "you win",
("paper", "paper"): "tie!",
("paper", "scissors"): "you lose",
("scissors", "rock"): "you lose",
("scissors", "paper"): "you win",
("scissors", "scissors"): "tie!",
}
print(outcomes[x, y])
首先,变量playachoice
是本地变量,而不是全局变量,这意味着您只能从playanput()中访问。因此,您必须先从playanput()
返回变量,然后才能分配x=playanput()
第二,为什么您试图将playachoice
分配给x?该变量将包含“您选择了摇滚”
等等。我认为您需要返回playerinput
,以便在下面进行比较。因此,在playanput()的末尾添加print playachoice
和return playerinput
第三,不必将playachoice
初始化为空字符串,然后在if-else子句中添加以下字符串。我认为你应该可以直接将“你选择了摇滚乐”等分配给playachoice
第四,使用raw\u input()
而不是input()
<代码>输入()
第五,您还应该从choose(x)
我认为这应该行得通
import random
choices = ["rock", 'paper', 'scissors']
def playainput():
playerinput = raw_input("Input rock, paper, or scissors: ")
if playerinput == ("rock"):
playachoice = ("you chose rock")
elif playerinput == ("paper"):
playachoice = ("you chose paper")
elif playerinput == ("scissors"):
playachoice = ("You chose scissors")
else:
print ("oops type again")
playainput()
return playerinput
def choose(x):
choice = random.choice(x)
print ("I chose %s" %choice)
return choice
x = playainput()
y = choose(choices)
if x == ("rock") and y == ("scissors"):
print ("you win")
if x == ("rock") and y == ("paper"):
print ("you lose!")
if x == ("rock") and y == ("rock"):
print ("tie")
if x == ("paper") and y == ("scissors"):
print ("I win!")
if x == ("paper") and y == ("paper"):
print ("tie!")
if x == ("paper") and y == ("rock"):
print ("you win!")
if x == ("scissors") and y == ("scissors"):
print ("tie!")
if x == ("scissors") and y == ("paper"):
print ("you win!")
if x == ("scissors") and y == ("rock"):
print ("you lose!")
请在您的答案中直接包含完整的回溯、逐字记录。谢谢。旁注:所有围绕str
literals的随机参数都是毫无意义的,只会让代码更加混乱(它们对行为没有任何影响)。你为什么认为你需要它们?这很有效!谢谢你帮了一个python新手。
import random
choices = ["rock", 'paper', 'scissors']
def playainput():
playerinput = raw_input("Input rock, paper, or scissors: ")
if playerinput == ("rock"):
playachoice = ("you chose rock")
elif playerinput == ("paper"):
playachoice = ("you chose paper")
elif playerinput == ("scissors"):
playachoice = ("You chose scissors")
else:
print ("oops type again")
playainput()
return playerinput
def choose(x):
choice = random.choice(x)
print ("I chose %s" %choice)
return choice
x = playainput()
y = choose(choices)
if x == ("rock") and y == ("scissors"):
print ("you win")
if x == ("rock") and y == ("paper"):
print ("you lose!")
if x == ("rock") and y == ("rock"):
print ("tie")
if x == ("paper") and y == ("scissors"):
print ("I win!")
if x == ("paper") and y == ("paper"):
print ("tie!")
if x == ("paper") and y == ("rock"):
print ("you win!")
if x == ("scissors") and y == ("scissors"):
print ("tie!")
if x == ("scissors") and y == ("paper"):
print ("you win!")
if x == ("scissors") and y == ("rock"):
print ("you lose!")