Python Rock Paper Scissors错误

时间:2017-05-11 02:08:40

标签: python

我在尝试运行此错误时遇到关于x未定义的错误。我是python的新手。任何帮助,将不胜感激。任何使这个更干净的代码的方法都会很棒。

https://pastebin.com/Kgw3gNsV

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!")

2 个答案:

答案 0 :(得分:1)

您的变量playachoice是函数playainput()的本地变量。所以在playainput()结束时添加:

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])

答案 1 :(得分:0)

首先,变量playachoice是本地的而不是全局的,这意味着您只能从playainput()内访问。因此,您必须先从playainput()返回变量,然后才能指定x = playainput()

其次,为什么要尝试将playachoice分配给x?该变量将包含"you chose rock"等等。我认为您需要返回playerinput,以便您在下面进行比较是有道理的。因此,将print playachoicereturn playerinput添加到playainput()

的末尾

第三,您不必将playachoice初始化为空字符串,然后在if-else子句中添加下面的字符串。我认为你应该可以直接指定&#34;你选择摇滚&#34;等等来玩游戏。

Forth,使用raw_input()代替input()input()只能在输入整数,浮点数等时使用。

第五,您还应该从choice

返回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!")