I'm trying to use a function from another file to generates random values for some variables but it doesn't seem to work
Function
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
Code that uses function (file name that function is in is named monsterstats.py)
from monsterstats import *
common_monsterstat()
def battlesys():
print("You challenged Monster!")
print("Monster's health: " mhealth)
CodePudding user response:
You've got a problem with scope. When you create a function, every variable you assign a value to is a local variable (unless you use the global
keyword). You could use the global keyword like this :
mhealth=0
def common_monsterstat():
global mhealth
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = health
def battlesys():
print("You challenged Monster!")
print("Monster's health: " mhealth)
common_monsterstat()
This will make your code change the global health instead of creating a copy of it...
But the better way to do this is to simply return the health variable, because global keywords can become confusing after a while in my experience. However, if you need your game to be really efficient, which you probably don't, using global would save you from creating too many variables ( although not sure if it would be more efficient I just think so :)) So, you could do it like this:
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return mhealth
def battlesys():
mhealth=common_monsterstat()
print("You challenged Monster!")
print("Monster's health: " mhealth)
common_monsterstat()
battlesys()
This will create a local
variable, assign it a value, return it into another variable, and then print it... Seems more complex but is actually easier...
Tell me if you need any clarifications! PS: You might wanna have a random name for your monster and use an f-string to name in a cooler way:
monsters=['centaurus','dwarf','spider']
def battlesys():
print(f"You challenged {r.choice(monsters)}!")
print("Monster's health: " health)
PPS: you didn't import random in your code...
CodePudding user response:
Inside the def common_monsterstat()
function add a return statement for mmaxhealth. Then at the second code assign mhealth to common_monsterstat()
function. I recommend u to learn about function, global and local variable at python :)
Here is the fix code:
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return mmaxhealth
from monsterstats import *
mhealth= common_monsterstat()
def battlesys():
global mhealth
print("You challenged Monster!")
print("Monster's health: " mhealth)
CodePudding user response:
If that's just setting up data, don't use a function. Yu can define constants that get imported by name. In "monsterstatps.py":
import random
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
Then in your "main.py":
from monsterstats import *
def battlesys():
print("You challenged Monster!")
print("Monster's health: " mhealth)
In the long term, you might define a class Stats
that contains these values, and import an instance of that class.
CodePudding user response:
Instead of having all free floating variables, it might be easier to group them in a class. For example you can have a class for generic stats, and create a factory function that creates instances of that class.
class Stats:
def __init__(self, attack, defense, health, maxhealth):
self.attack = attack
self.defense = defense
self.health = health
self.maxhealth = maxhealth
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return Stats(
attack=mattack,
defense=mdefense,
health=mhealth,
maxhealth=mmaxhealth,
)
In the main method you can then use the factory method to retrieve the stats:
from monsterstats import *
stats = common_monsterstat()
def battlesys():
print("You challenged Monster!")
print("Monster's health: " stats.health)
To make writing this class even easier, you could take advantage of a dataclass, which is perfect for pure data objects.
from dataclasses import dataclass
@dataclass
class Stats:
attack: int
defense: int
health: int
maxhealth: int
...