loops - Looping and ignoring flag - Python -
i working on learning python , decided write small battle engine use few of different items have learned make bit more complicated. problem having set selection user makes should cause 1 of either 2 parts load, instead skips loop instead of performing selection. here have far:
import time import random import sys player_health = 100 enemy_health = random.randint(50, 110) def monster_damage(): mon_dmg = random.randint(5,25) enemy_health - mon_dmg print ('you hit beast ' + str(mon_dmg) + ' damage! brings health ' + str(enemy_health)) player_dmg() def player_dmg(): pla_dmg = random.randint(5,15) player_health - pla_dmg print ('the beast strikes out ' + str(pla_dmg) + ' damage you. leaves ' + str(player_health)) def run_away(): run_chance = random.randint(1,10) if run_chance > 5: print ('you escape beast!') time.sleep(10) sys.exit else: print ('you try run , fail!') player_dmg() def player_turn(): print ('your turn:') print ('your health: ' + str(player_health) + ' monsters health: ' + str(enemy_health)) print ('what next action?') print ('please select 1 attack or 2 run.') action = input() if action == 1: monster_damage() elif action == 2: run_away() while player_health > 0 , enemy_health > 0: player_turn() if player_health <= 0: print ('the beast has vanquished you!') time.sleep(10) sys.exit elif enemy_health <= 0: print ('you have vanquished beast , saved our chimichongas') time.sleep(10) sys.exit
the function input
returns str
not int
action = input()
so comparison return false
if action == 1:
for example
>>> '1' == 1 false
you can convert input int
follows
action = int(input())
Comments
Post a Comment