名称错误——未定义

发布于 2024-11-18 09:11:58 字数 1833 浏览 9 评论 0原文

主程序

# 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()

第二个文件:wining.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

错误

Traceback (most recent call last):
  File "C:/Python32/RPS_Project/Main.py", line 14, in <module>
    ROCK = r
NameError: name 'r' is not defined

我已经尝试了引号和所有,但无法弄清楚!有什么帮助吗? 请并谢谢您!

Main Program

# 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()

Second file: 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

Error

Traceback (most recent call last):
  File "C:/Python32/RPS_Project/Main.py", line 14, in <module>
    ROCK = r
NameError: name 'r' is not defined

I have tried quotation marks and all, and cannot figure this out!!! any help with this?
Please and thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2024-11-25 09:11:58

不要以错误的方式对待负面评论。请务必将您的作业标记为作业,并粘贴您发布的代码生成的实际错误。此错误与您发布的代码不匹配。

你也可以以一种看起来稍微平静的方式提出你的问题:)

问题很简单。您在主程序中定义全局变量,但不在 wining.py 中定义,因此类似的行将

if userInput == ROCK and computerInput == SCISSOR:
    print('You win!  Rock crushes Scissor!')
    win += 1

导致 NameErrors,因为 ROCKSCISSORwin 未定义。在每个模块中,您必须定义或导入您想要使用的所有名称;名称不会在模块之间自动共享——这是有充分理由的!

我会告诉您还必须从 winner.winner返回一个值,从而为您省去一些麻烦 - 否则,您将无法获得预期的输出。

Don't take the negative comments the wrong way. Just be sure to mark your homework as homework and be sure to paste the actual error generated by the code that you've posted. This error doesn't match the code you posted.

You might also ask your question in a slightly calmer-seeming way :)

The problem is very simple. You define your globals in the main program but not in winning.py, so lines like

if userInput == ROCK and computerInput == SCISSOR:
    print('You win!  Rock crushes Scissor!')
    win += 1

are going to cause NameErrors, because ROCK, SCISSOR and win are not defined. In every module, you must either define or import all the names you want to use; names are not shared between modules automatically -- for good reason!

I'll save you some trouble by telling you that you must also return a value from winning.winner -- otherwise, you won't get the output you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文