我得到這個錯誤約x試圖運行時沒有被定義。我是python的新手。任何幫助,將不勝感激。任何方式來使這個更乾淨的代碼將是偉大的。Python岩石紙剪刀錯誤
import random
choices = ["rock", 'paper', 'scissors']
def playainput():
playachoice = ("")
playerinput = 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()
print (playachoice)
def choose(x):
choice = random.choice(x)
print ("I chose %s" %choice)
x = playachoice
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'文字的隨機零件都是毫無意義的,只是讓代碼更加混亂(它們對行爲沒有任何影響)。你爲什麼認爲你需要他們? – ShadowRanger