2016-09-15 73 views
-3

所以即时尝试编写一个基本的文本为基础的战斗思考,但它不允许我打电话给我的功能,这是我的代码。NameError,功能未定义

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

我的错误说: 回溯(最近通话最后一个): 文件 “AdventureLibs.py” 46行,在 fight_start() NameError:名字 'fight_start' 没有定义

我在网上找不到修复工作,有人可以帮我吗?

+3

'fight_start'在'game_start'内缩进,所以它没有在全局范围中定义 –

回答

1

正如Adam Smith在评论中提到的,fight_startgame_start的一个内部函数。它隐藏在全球范围内,无法在game_start之外访问。我不知道你是否打算这么做,但最简单的解决方法是取消缩进fight_start