2016-09-15 72 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