每当我调用我的start()函数时,在声明之后,我会收到错误消息,说它不存在。我的完整代码如下。
import random
import time
global monsters
monsters = ['Goblin','Orc','Knight','Unicorn','Black Bear','White Dragon','Black Dragon','Dark Knight','Undead Corpse']
global player_level
player_level = 1
global player_exp
global monster_level
monster_level = monster_determ_easy
player_exp = 0
player_exp_n = 10
global monster_attack
global player_attack
player_attack = random.randint(1,4) + player_level
global monster_levels
global monster_determ_easy
global monster_determ_medium
global monster_determ_hard
global monster_determ_epic
global attacks
global gold
gold = 0
attacks = ['Punch','Fireball']
monster_determ_easy = random.randint(1,3)
monster_levels_easy = player_level - monster_determ_easy
monster_determ_medium = random.randint(1,3)
monster_levels_medium = player_level + monster_determ_medium
monster_determ_hard = random.randint(3,5)
monster_levels_hard = player_level + monster_determ_hard
monster_determ_epic = random.randint(5,10)
monster_levels_epic = player_level + monster_determ_epic
monster_attack = monsters_levels + random.randint(1,3)
def start():
if player_exp>=player_exp_n:
player_level=player_level+1
print "You've reached level ",player_level
gold=gold+random.randint(1,15) + player_level
print 'You were awared ',' gold!'
start()
command = raw_input(">")
if command == "fight":
player_attack = random.randint(1,4) + player_level
monster_attack = monsters_levels + random.randint(1,3)
choose_monster = random.choose(monsters)
print 'You encounter a level ',monster_determ_easy,choose_monster,'.'
time.sleep(1)
print 'Prepare to fight!'
strike = random.random()
if strike>=0.5:
if player_attack >= monster_attack:
print 'You slained the ',choose_monster,'!'
exp_choose = random.randint(1,5) + player_level
player_exp = player_exp + exp_choose
print 'You were awarded ',exp_choose,' XP!'
start()
start()
# This was just added
答案 0 :(得分:2)
因为第一个if语句的计算结果为false,所以你永远不会有机会设置命令变量。
答案 1 :(得分:1)
因为它只是从内部调用。