2016-05-10 10 views
0

我努力讓自己在Python中的石頭,剪子,布的計劃,但每當我跑我的程序它給了我一個錯誤說這個,當我想重新運行我的某個函數時,爲什麼會出現錯誤?

File "C:/Users/Home/PycharmProjects/untitled/hi.py", line 59, in <module> 
    play_again = input("Do you want to play again? y/n") 
    File "<string>", line 1, in <module> 
NameError: name 'y' is not defined 

我知道你可能會想我的代碼,所以這裏是,

from random import * 
import os, pygame 
moves = ['Rock','Paper','Scissors'] 


def game(): 

    move=choice(moves) 
    player = input("pick a move, 1. Rock, 2. Paper, 3. Scissors") 


    if (player == 1 and move == 1): 

     print(move, player) 
     print("It is a tie!") 

    if (player == 1 and move == 2): 

     print (move, player) 
     print("The computer wins!") 

    if (player == 1 and move == 3): 

     print (move, player) 
     print("You win!") 

    if (player == 2 and move == 1): 

     print (move, player) 
     print("You win!") 

    if (player == 2 and move == 2): 

     print (move, player) 
     print("It's a tie!") 

    if (player == 2 and move == 3): 

     print (move, player) 
     print("The computer wins!") 

    if (player == 3 and move == 2): 

     print (move, player) 
     print("You win!") 

    if (player == 3 and move == 3): 

     print (move, player) 
     print("It's a tie!") 

    if (player == 3 and move == 1): 

     print (move, player) 
     print("The computer wins!") 

game() 

play_again = input("Do you want to play again? y/n") 

if(play_again == "y"): 
    game() 

請問你能幫我嗎?謝謝

+0

您正在運行python 2並且'input'沒有按照您的想法進行操作。在python 2你應該使用'raw_input' – jgritty

+0

我確信這是我運行的代碼 – zane

+0

運行* ....... – zane

回答

2

您正在運行Python2。 Python2中的input嘗試將用戶條目解釋爲代碼。 y並不意味着任何東西(除非你聲明瞭一個變量y,但不管它是不是"y"),這是一個NameError123意味着什麼(在你的代碼前面使用)

你應該總是在的Py2,其解釋用戶的條目作爲字符串中使用raw_input。您必須將之前的if語句調整爲字符串(if player == "1" and moves == 1等)

相關問題