2017-05-11 19 views
0

これを実行しようとすると、xが定義されていないというエラーが表示されます。私はPythonの初心者です。どんな助けもありがとう。また、このよりクリーンなコードを作成する方法は素晴らしいでしょう。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!") 
+0

を動作するはずだと思う逐語的に、完全なトレースバックを含めてくださいあなたの答えに直接。ありがとう。 –

+0

サイドノート: 'str'リテラルを囲むこれらのランダムな括弧は無意味で、コードをもっと混乱させます(ビヘイビアには全く影響しません)。なぜあなたはそれらが必要だと思いますか? – ShadowRanger

答えて

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

これは機能します! Pythonの新しい男を助けてくれてありがとう。 –

0

最初に変数playachoiceローカルで、グローバルではありません。つまり、playainput()の中からのみアクセスできます。したがって、x = playainput()を割り当てる前に、変数をplayainput()から戻す必要があります。

次に、playachoiceをxに割り当てようとしていますか?その変数には"you chose rock"などが含まれます。私はplayerinputを返す必要があると思うので、下の比較が意味をなさなくなります。だから、playainput()

第三の終わりにprint playachoicereturn playerinputを追加するには、空の文字列にplayachoiceを初期化し、その後のif-else節では、以下の文字列を追加する必要はありません。私はあなたが演奏選曲に "あなたが選んだ岩"などを直接割り当てることができるべきだと思います。

の代わりにraw_input()を使用してください。 input()だけあなたが入力整数にしたい場合など

フィフス浮い使用することができ、あなたもchoose(x)

からchoiceを返す必要があり、私はこれが

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!") 
関連する問題