Why doesn't my Python Function start when called upon?
Whenever I'm calling my start() function, after it's been declared, I get a error saying it doesn't exist. My complete code is below.
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
p开发者_如何转开发layer_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
Because the first if statement evaluates to false, so you never have the opportunity to set the command variable.
Because it's only called from within itself.
精彩评论