'Python - My while loop not generating random numbers in a function called inside the while loop
I'm trying to get the while loop to randomize a monster stats and its name on a function called inside the loop, it creates it onces but it's not randomieze everytime the while completes a loop.
Everytime I run the Mobs() outside the loop, the mobs are randomize, yet somehow it doesn't work on inside the loop.
import random
import time
mobs = ['rat', 'snake', 'bat', 'spider', 'mole', 'hawk', 'fox', 'vulture', 'beetle']
mobs_names = random.choice(mobs)
mob_hp = random.randint(45, 60)
mob_damage = random.randint(8, 13)
mob_exp = random.randint(20, 23)
class Mobs:
def __init__(self, hp=mob_hp, damage=mob_damage, name=mobs_names, exp=mob_exp):
self.hp = hp
self.damage = damage
self.name = name
self.exp = exp
print("Monster is a " + self.name.title() + ' and it\'s stats are...')
print('HP: ' + str(self.hp))
print('Attack Damage: ' + str(self.damage))
gameloop = True
#spawn_mobs = Mobs()
world_level = 0
battlebegun = print("Battle has started")
while gameloop == True:
while world_level < 10:
world_level += 1
print(world_level)
if world_level <= 10:
spawn_mobs = Mobs()
battlebegun
time.sleep(1)
continue
break #gameloop = False
print("Game over")
Solution 1:[1]
You are only randomizing the values once because they are not being called inside the loop. It's like doing this:
random_var = random.randint(1, 10)
while True:
print(random_var)
Output:
5, 5, 5, 5, 5, 5, 5
as opposed to this:
while True:
random_var = random.randint(1, 10)
print(random_var)
Output:
3, 2, 7, 4, 6, 6, 9
In the first example, the random function is called once because it isn't in the while loop, but in the second example it keeps being randomized. Here is your code fixed with the randomization inside the loop.
import random
import time
mobs = ['rat', 'snake', 'bat', 'spider', 'mole', 'hawk', 'fox', 'vulture', 'beetle']
# You can still keep this if you want in order to have random initial values.
mobs_names = random.choice(mobs)
mob_hp = random.randint(45, 60)
mob_damage = random.randint(8, 13)
mob_exp = random.randint(20, 23)
class Mobs:
def __init__(self, hp=mob_hp, damage=mob_damage, name=mobs_names, exp=mob_exp):
self.hp = hp
self.damage = damage
self.name = name
self.exp = exp
print("Monster is a " + self.name.title() + ' and it\'s stats are...')
print('HP: ' + str(self.hp))
print('Attack Damage: ' + str(self.damage))
gameloop = True
#spawn_mobs = Mobs()
world_level = 0
battlebegun = print("Battle has started")
while gameloop == True:
while world_level < 10:
world_level += 1
print(world_level)
if world_level <= 10:
# Randomizes mob values inside loop.
mobs_names = random.choice(mobs)
mob_hp = random.randint(45, 60)
mob_damage = random.randint(8, 13)
mob_exp = random.randint(20, 23)
spawn_mobs = Mobs()
battlebegun
time.sleep(1)
continue
break #gameloop = False
print("Game over")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | joshdimatteo |
