How do I reset the value of variables in a loop? (Python 3.1.1) -
when run code , battle part of game, random values assigned characters attack, defense, damage , health. however, after first turn, keep getting same values , can't reset.
for example, user's attack random number 4 11. program "rolls" 5 , assigns variable useratk
useratk = random.randint(4,11)
i assumed everytime loop ran, generate new value. not, , every time print variable same value first assigned. missing something?
below code
import random # variables # # # variable user's character name username = input("brave warrior, name? ") # variable used in input function pause game enternext = ("press enter continue...") # variable used format user input prompt # battle text prompt = ">>>>> " # variable used add new line string newline = "\n" # variable used display message when hero dies herodeadmsg = username + " has fallen!" # these variables represent dragon's stats (hp, atk & def) dragonhp = 100 dragonatk = random.randint(5,10) dragondef = random.randrange(8) # these variables represent user's stats (hp, atk & def) userhp = 90 useratk = random.randint(4,11) userdef = random.randrange(8) # these variables calculate battle damage , hp dragondmg = (useratk - dragondef) dragonhp -= dragondmg userdmg = (dragonatk - userdef) userhp -= userdmg # variable prints options in battle menu battlemenu = """attack (a) - magic (m) - item (i) - run (r)""" # variable determines goes first cointoss = random.randint(0, 1) # these variables print actions in each turn dragonattack = \ prompt + "crimson dragon attacks " + str(dragonatk) + " atk!"\ + newline + prompt + "you defend " + str(userdef) + " def!"\ + newline + prompt userattack = \ prompt + "you attacked " + str(useratk) + " atk!"\ + newline + prompt + "crimson dragon defends " + str(dragondef) + " def!"\ + newline + prompt usermagic = \ prompt + username + " tried use magic!"\ + newline + prompt + username + " has no magic!"\ + newline + prompt useritem = \ prompt + username + " tried use item!"\ + newline + prompt + username + " has no items!"\ + newline + prompt userretreat = \ prompt + username + " tries retreat!"\ + newline + prompt + "the enemy won't let escape!"\ + newline + prompt # these variables show health during battle printdragonhp = "crismon dragon has " + str(dragonhp) + " hp remaining!" printuserhp = username + " has " + str(userhp) + " hp remaining!" # variable simulates results of coin toss cointoss = random.randint(0, 1) # # # condtitions # # # these conditions determines attacks first if cointoss == 0: currentturn = "dragon" elif cointoss == 1: currentturn = "user" else: print("the coin toss failed!") # # # battle mechanics # # while currentturn: # mechanics crimson dragon's turn if currentturn == "dragon": # prints crimson dragon's attack , ends turn print(newline + prompt + "crimson dragon moves!"\ + newline + prompt + newline + dragonattack\ + newline + prompt + username + " takes " + str(userdmg) + " dmg!"\ + newline + prompt + printuserhp) currentturn = "user" input(prompt) # need implent way reset atk , def # mechanics user's turn if currentturn == "user": # prints battle menu , asks user's choice print(newline + prompt + battlemenu\ + newline + prompt) userchoice = input(prompt) # prints user's attack , ends turn if userchoice == "a": print(userattack) if userhp < 1: print(herodeadmsg) break input (prompt) currentturn = "dragon" # prints user's magic , ends turn elif userchoice == "m": print(usermagic) input (prompt) currentturn = "dragon" # prints user's item , ends turn elif userchoice == "i": print(useritem) input (prompt) currentturn = "dragon" # prints user's retreat , ends turn elif userchoice == "r": print(userretreat) input (prompt) currentturn = "dragon" # prints error message invalid entries else: print(newline + prompt + "that not valid menu item."\ + newline + prompt + "please try again.")
random.randint(4,11)
chooses integer in range [4, 11]
, returns number. so, when useratk = random.randint(4,11)
, you're getting number , storing useratk
, , every time access useratk
you'll same number back.
if hoping useratk
magic kind of thing acts different number in range [4, 11]
each time access it… well, that's not impossible (see here quick & dirty stab @ it)… lead more confusion benefit.
for example, have code tries print out str(useratk)
… but if value different each time access it, gets printed out different what's used calculate damage! imagine if playing tabletop d&d, , dungeon master rolled die tell roll, , forgot result , rolled again figure out whether hit. might say, "you rolled 20. missed." that's no good.
what might useful make useratk
function:
def useratk(): return random.randint(4, 11)
and of similar variables. then, everywhere accessing number, you'll instead call function:
def dragondmg(): return useratk() - dragondef()
then somewhere, you're going want store results of calling functions in local variables within each loop.
but key that, it, have have variables re-calculate each time through loop.
Comments
Post a Comment