Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NameError--未定义_Python_Variables_Definition_Nameerror - Fatal编程技术网

Python NameError--未定义

Python NameError--未定义,python,variables,definition,nameerror,Python,Variables,Definition,Nameerror,主程序 # import statements import random import winning # Set constants win = 0 lose = 0 tie = 0 ROCK = 1 PAPER = 2 SCISSOR = 3 # Main Program for the Rock Paper Scissor game. def main(): # set variable for loop control again = 'y' while

主程序

# import statements
import random
import winning

# Set constants
win = 0
lose = 0
tie = 0

ROCK = 1
PAPER = 2
SCISSOR = 3

# Main Program for the Rock Paper Scissor game.
def main():
    # set variable for loop control
    again = 'y'

    while again == 'y':
        # Display menu
        display_menu()
        # prompt for user input
        userSelection = input('Which would you like to play with (1, 2, 3)?: ') 
        computerSelection = random.randint(1, 3) 

        # Call winner module to decide the winner!
        print(winning.winner(userSelection, computerSelection))

        # Ask to play again and make selection
        again = input('Would you like to play again (y/n)?')


def display_menu():
    print('Please make a selection: ')
    print(' 1) Play Rock')
    print(' 2) Play Paper')
    print(' 3) Play Scissor')



# Call main
main()
第二个文件:winning.py:

# This module will decide on who won based on input from Main

def winner(userInput, computerInput):
    if userInput == ROCK and computerInput == SCISSOR:
        print('You win!  Rock crushes Scissor!')
        win += 1
    elif userInput == SCISSOR and computerInput == PAPER:
        print('You win!  Scissor cuts Paper!')
        win += 1
    elif userInput == PAPER and computerInput == ROCK:
        print('You win!  Paper covers Rock!')
        win += 1
    elif userInput == computerInput:
        print('You tied with the computer! Please try again!')
        tie += 1
    else:
        print('You lost! Please try again!')
        lose += 1
错误

回溯(最近一次呼叫最后一次):
文件“C:/Python32/RPS_Project/Main.py”,第14行,在
岩石=r
NameError:未定义名称“r”
我已经试过引号和所有的,但无法解决这个问题!!!有什么帮助吗?
请,谢谢

不要错误地接受负面评论。只需确保将您的作业标记为作业,并确保粘贴由您发布的代码生成的实际错误即可。此错误与您发布的代码不匹配

你也可以稍微平静地问你的问题:)

问题很简单。您可以在主程序中定义全局变量,但不能在
winning.py
中定义,因此

if userInput == ROCK and computerInput == SCISSOR:
    print('You win!  Rock crushes Scissor!')
    win += 1
将导致名称错误,因为未定义
ROCK
SCISSOR
win
。在每个模块中,必须定义或导入所有要使用的名称;模块之间不会自动共享名称——这是有充分理由的


我会告诉您,您还必须
返回
winning.winner
中的值,这样可以省去一些麻烦——否则,您将无法获得预期的输出

请提供连贯的资料。回溯与代码没有任何共同之处……再一次:我们没有做你的工作homework@Alli犯错误“ROCK=r”不在您发布的代码中。请把实际坏掉的代码贴出来。正如帖子所说,它应该运行良好,因为它将ROCK设置为1。不要求任何人做我的家庭作业。我只是请求帮助学习。很抱歉,我将不再使用此网站寻求帮助,请理解我做错了什么。。。谢谢您,并对给您带来的不便表示歉意。@Alli:您发布的代码不会生成您发布的错误。@Alli!您还必须定义
lose
!您必须定义您使用的每个变量名。非常感谢!!从现在起,我将确保所有帮助的标题中都有“家庭作业”。谢谢Senderle…你帮了我很大的忙。
if userInput == ROCK and computerInput == SCISSOR:
    print('You win!  Rock crushes Scissor!')
    win += 1