2017-05-11 44 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

This Works!謝謝你幫助一個蟒蛇新人。 –

0

首先變量playachoice是本地的而不是全球的,這意味着您只能從playainput()以內訪問。因此,您必須先返回playainput()中的變量,然後才能指定x = playainput()

其次,你爲什麼試圖將playachoice分配給x?該變量將包含"you chose rock"等。我認爲你需要返回playerinput,以便下面的比較有意義。所以加print playachoicereturn playerinputplayainput()

第三節結束時,你不必初始化playachoice爲空字符串,然後在if-else子句在下面添加字符串。我認爲你應該能夠直接指定「你選擇的搖滾樂」等來玩樂。

第四,用raw_input()代替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!")