0

好吧,我几乎是一个全新的高中程序员,有大约半年的经验(在 Python 中)。我正在制作一个基本的文字游戏,遇到了一个我花了几个小时但似乎无法解决的错误。抱歉,如果我以错误的方式提出任何问题,这是我在这里的第一个问题。

这是我的代码:

############################################################################
#               APOCALYPSE GAME - TEXT EDITION                             #         
#               version 0.0.1                                              #
############################################################################
### BEGINNING TEXT. ###
print('Hello, and welcome to my apocalypse game - text edition!')
print()
print('This is my first game, and I would like to make a graphical version')
print('later in my career.')
print()
print('But, for now, here is the text edition of it so you can get a taste')
print('of what is to come.')
print()
print('NOTE: PLEASE ENTER ALL TEXT IN ALL CAPS.')

print('CHARACTER SETUP:')
print()
def gender(): ### ALLOWS USER TO PICK A GENDER FOR THERE CHARACTER.
    womanOrMan = input('Are you a woman or a man(answer WOMAN/MAN)?')
    if womanOrMan == 'MAN':
        print('You are a man.')
        customizationMan()
    elif womanOrMan == 'WOMAN':
        print('You are a woman.')
        customizationWoman()
    else:
        print('Invalid answer. Please try again.')
        gender()

def customizationMan(): ### CHARACTER CUSTOMIZATION FOR MEN. ###
    print('NOTE: IN THIS AREA YOU MAY USE LOWER CAPS. I WILL TELL YOU WHEN TO USE CAPS AGAIN.')
    hair = input('What hairstyle do you have?')
    hairColor = input('What is the color of your hair?')
    eyeColor = input('What is the color of your eyes?')
    noseSize = input('How big is your nose(inches)?')
    beard = input('What is the name of your beard(if none say "NONE")?')
    bodyType = input('What is your body type?')
    race = input('What is your race?')
    clothes = input('What are you wearing?')

    print()
    print('Here is a summary of you: ')
    print()
    print('You have a ' + hair + ' hairstyle, and you the color of it is ' + hairColor + '.')
    print('Your eyes are the color ' + eyeColor + '.')
    print('Your nose is ' + noseSize + ' inches long.')
    if beard == 'NONE':
        print('You are clean shaven.')
    else:
        print('You have a ' + beard + ' beard.')
    print('You have a ' + bodyType + ' body type.')
    print('You are of ' + race + ' background.')
    print('You are wearing ' + clothes + '.')
    characterConfirmation()


def customizationWoman():### CHARACTER CUSTOMIZATION FOR WOMEN.###
    print('NOTE: IN THIS AREA YOU MAY USE LOWER CAPS. I WILL TELL YOU WHEN TO USE CAPS AGAIN.')
    hair = input('What hairstyle do you have?')
    hairColor = input('What is the color of your hair?')
    eyeColor = input('What is the color of your eyes?')
    noseSize = input('How big is your nose(inches)?')
    bodyType = input('What is your body type?')
    race = input('What is your race?')
    clothes = input('What are you wearing?')

    print()
    print('Here is a summary of you: ')
    print()
    print('You have a ' + hair + ' hairstyle, and you the color of it is ' + hairColor + '.')
    print('Your eyes are the color ' + eyeColor + '.')
    print('Your nose is ' + noseSize + ' inches long.')
    print('You have a ' + bodyType + ' body type.')
    print('You are of ' + race + ' background.')
    print('You are wearing ' + clothes + '.')
    characterConfirmation()

def characterConfirmation(): ### CONFIRMS USER WANTS HIS CHARACTER TO LOOK LIKE HIS SELECTIONS FOR SURE. ###
    print('BEGIN TO ANSWER IN ALL CAPS ONCE AGAIN.')
    print('Are you sure this is what you want your character to look like(if you')
    print('enter "YES" you will move on. If you enter "NO" the character creation')
    print('process will restart)?')
    characterRestart = input('Enter "YES" or "NO" now:')
    if characterRestart == "YES":
        attributeSelection()
    elif characterRestart == "NO":
        print('Restarting character creation.')
        gender()
    else:
        print('Invalid answer. Please try again.')
        print()
        print()
        characterConfirmation()


gender()




#####################################################################################################################################
#####################################################################################################################################
#                                                           CHARACTER ATRRIBUTES                                                    #
#####################################################################################################################################
#####################################################################################################################################


def attributeSelection():
    print("Ok. Now it is time to set your character attributes and then you can begin the game")
    print()
    print("You're character attributes are very important. They can determine whether or not")
    print("you survive a certain situation.")
    print("YOU HAVE 25 SKILL POINTS. SPEND THESE ON THE FOUR ATTRIBUTES")
    print("STRENGTH, VITALITY, STEALTH, AND EXPERIENCE.")
    print()
    print()
    print("Strength makes you able to kill enemies faster, and to do tasks that")
    print("you normally coulden't.")
    print()
    print("Vitality makes you able to survive longer in combat situations.")
    print()
    print("Stealth makes you more likely to beable to avoid cobat situations when they are not")
    print("necessary and sneak up on enemies when they are.")
    print()
    print("Experience makes you more likely to be able to survive bad weather conditions,")
    print(" enhances your ability to preserve food, and helps you survive on your own")
    print(" in the wild in general.")
    print()
    print()
    print("TIME TO SELECT YOUR ATTRIBUTES:")
    print()

    balance = 25
    print("Your SP balance is currently 25.")
    strength = input("How much SP do you want to put into strength?")
    balanceAfterStrength = balance - strength
    if balanceAfterStrength == 0:
        print("Your SP balance is now 0.")
        attributeConfirmation()
    elif strength < 0:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif strength > balance:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif balanceAfterStrength > 0 and balanceAfterStrength < 26:
        print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.")
    else:
        print("That is an invalid input. Restarting attribute selection.")
        attributeSelection()

    vitality = input("How much SP do you want to put into vitality?(" + balanceAfterStrength + " left!)")
    balanceAfterVitality = balanceAfterStrength - vitality
    if balanceAfterVitality == 0:
        print("You SP balance is now 0.")
        attributeConfirmation()
    elif vitality < 0:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif vitality > balanceAfterStrength:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif balanceAfterVitality > 0 and balanceAfterVitality < 26:
        print("Ok. You're balance is now at " + balanceAfterVitality + " skill points.")
    else:
        print("That is an invalid input. Restarting attribute selection.")
        attributeSelection()

    stealth = input("How much SP do you want to put into stealth?(" + balanceAfterVitality + " left!)")
    balanceAfterStealth = balanceAfterVitality - stealth
    if balanceAfterStealth == 0:
        print("Your SP balance is now 0.")
        attributeConfirmation()
    elif stealth < 0 :
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif stealth > balanceAfterVitality:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif balanceAfterStealth > 0 and balanceAfterStealth < 26:
        print("Ok. You're balance is now at " + balanceAfterStealth + " skill points.")
    else:
        print("That is an invalid input. Restarting attribute selection.")
        attributeSelection()

    experience = input("How much SP do you want to put into experience?(" + balanceAfterStealth + " left!")
    balanceAfterExperience = balanceAfterStealth - experience
    if balanceAfterExperience == 0:
        print("Your SP balance is now 0.")
        attributeConfirmation()
    elif experience < 0:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif experience > balanceAfterStealth:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif balanceAfterExperience > 0 and balanceAfterStealth < 26:
        print("Oops! You did not spend all of your skill points. Restarting attribute selection.")
        print(" remember to spend all of them this time!")
        attributeSelection()
    else:
        print("That is an invalid input. Restarting attribute selection.")
        attributeSelection()

def attributeConfirmation():
    print("ATTRIBUTE CONFIRMATION TEST SUCCESS!")

问题是,当我在 shell 中运行代码时,会发生以下情况:

Hello, and welcome to my apocalypse game - text edition!

This is my first game, and I would like to make a graphical version
later in my career.

But, for now, here is the text edition of it so you can get a taste
of what is to come.

NOTE: PLEASE ENTER ALL TEXT IN ALL CAPS.
CHARACTER SETUP:

Are you a woman or a man(answer WOMAN/MAN)?MAN
You are a man.
NOTE: IN THIS AREA YOU MAY USE LOWER CAPS. I WILL TELL YOU WHEN TO USE CAPS AGAIN.
What hairstyle do you have?UGLY MULLET
What is the color of your hair?BROWN
What is the color of your eyes?BLUE
How big is your nose(inches)?5
What is the name of your beard(if none say "NONE")?NONE
What is your body type?ATHLETIC
What is your race?WHITE
What are you wearing?JEANS AND A TSHIRT

Here is a summary of you:

You have a UGLY MULLET hairstyle, and you the color of it is BROWN.
Your eyes are the color BLUE.
Your nose is 5 inches long.
You are clean shaven.
You have a ATHLETIC body type.
You are of WHITE background.
You are wearing JEANS AND A TSHIRT.
BEGIN TO ANSWER IN ALL CAPS ONCE AGAIN.
Are you sure this is what you want your character to look like(if you
enter "YES" you will move on. If you enter "NO" the character creation
process will restart)?
Enter "YES" or "NO" now:YES
Traceback (most recent call last):
    File "C:/Python32/APOCALYPSE GAME LIBRARY/apocalypseGame.py", line 96, in <module>
        gender()
    File "C:/Python32/APOCALYPSE GAME LIBRARY/apocalypseGame.py", line 22, in gender
        customizationMan()
    File "C:/Python32/APOCALYPSE GAME LIBRARY/apocalypseGame.py", line 54, in customizationMan
        characterConfirmation()
    File "C:/Python32/APOCALYPSE GAME LIBRARY/apocalypseGame.py", line 85, in characterConfirmation
        attributeSelection()
NameError: global name 'attributeSelection' is not defined

我不知道为什么会收到此错误。我认为它告诉我我没有定义函数attributeSelection,但我显然做了。我还检查了我所有的拼写......所以我很难过。有谁知道我该如何解决这个问题?谢谢您的帮助。

4

2 回答 2

3

gender()尝试移动代码底部的唯一行。

当它被调用时,attributeSelection还没有定义所需的方法。

于 2012-11-30T00:46:36.363 回答
0

jvivenot 已经回答了最初的问题,但我想为任何可能找到这篇文章并遇到类似问题的人提供更详细和通用的答案。

造成这类问题是因为 Python 不是编译语言,不像 Java 或 C。在编译语言中,代码是一种“预运行”;在您的任何代码实际运行之前,计算机会读取您的所有代码并构建您的所有功能。Python 不这样做。Python 按顺序运行每一行,一次一行,从上到下。这意味着 Python 不能向前看文件的底部。当一行代码尝试使用 Python 尚未看到的函数时,就会出现问题,因为它尚未读取该部分代码。

在问题中给出的代码中,在定义gender()之前运行。attributeSelection()Python 还没有看到attributeSelection(),据 Python 所知,它不存在。不幸的是,gender()尝试运行attributeSelection()但由于它不存在,Python 提出了 this nameerror

有一种简单的方法可以避免这种情况:始终在程序顶部执行所有定义,然后再执行主程序主体。这样,您可以确保 Python 在开始尝试运行您的主程序之前可以查看并创建您的所有函数。这也有助于使您的代码井井有条且易于阅读。

于 2012-11-30T01:25:32.313 回答