NameError,函数未定义

时间:2016-09-15 21:55:18

标签: python text-based

所以我试着编写一个基于文本的基础文本思考,但它不允许我调用我的函数,这是我的代码。

import random
import math
import sys
# Variables
health = 100
goblin_health = 100
# Intro
def game_start():
    print('As you are walking down the street, you see a goblin!')
    print('He draws his sword and dashes towards you!')
# Fight Sequence
    def fight_start():
        while True:
            print('Would you like to:\n1. Run\n2. Attack\n3. Block')
            action = input()
            if action == 1:
                print('You tried to run away like the coward you are, but there is no where to run.')
            goblindamage = random.randint(1, 9)
            health = health - goblin_damage
            print('You took ' + str(goblin_damage) + ' damage. You now have ' + str(health) + ' health left.')
            continue
        elif action == 2:
            damage = random.randint(10, 20)
            goblin_health = goblin_health - damage
            print('You attack the goblin for ' + str(damage) + ' damage. It has ' + str(goblin_health) + ' health left.')
            continue
        elif action == 3:
            block = random.randint(60, 90)
            goblin_damage = random.randint(1, 9)
            health = health - goblin_damage / block
            print('You blocked ' + str(block) + ' percent of the damage delt, you took ' + str(goblin_damage) + ' damage and have ' + str(health) + ' health left.')
            continue
        elif goblin_health <= 0:
            print('You killed the goblin! Congradulations, you win!')
            break
        elif health <= 0:
            print('You died! Restarting...')
            fight_start()
game_start()
print('It is time to fight! You draw your sword.')
fight_start()

我的错误说: Traceback(最近一次调用最后一次):   文件&#34; AdventureLibs.py&#34;,第46行,in     fight_start() NameError:name&#39; fight_start&#39;未定义

我无法找到有效的在线修复程序,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

正如亚当·斯密在评论中提到的那样,fight_startgame_start的内在功能。它隐藏在全局范围之外,无法在game_start之外访问。我不知道您是否打算这样做,但最简单的解决方法就是取消缩进fight_start