Oct-04-2021, 03:29 AM
(This post was last modified: Oct-04-2021, 03:30 AM by deanhystad.)
There are several Python types that can hold multiple values; lists, tuples, sets, dictionaries. Tuples would not be right at all because they are immutable. You cannot change the values in a tuple. Sets are also a bad choice because they are just unordered collections. You would not be able to get the health, because in a set there would be no way to specify which value in the set was health.
Lists could work. You could make characters like this:
Dictionary is similar to list, but instead of using an index to access the attribute values you use a key. The key does not have to be an integer like the list index. It can be an integer or a string or any "hashable" Python type.
Lists could work. You could make characters like this:
MAGE = 0 THIEF = 1 WARRIOR = 2 CHARACTER = 0 HEALTH = 1 STAMINA = 2 MAGIC = 3 bob = [MAGE, 7, 3, 17] jan = [THIEF, 11, 17, 2] bob[HEALTH] = max(0, bob[HEALTH] - random.randint(1,4))CHARACTER, HEALTH, STAMINA and MAGIC are indices into a character's attribute list. To get or set the attribute you use the list[index].
Dictionary is similar to list, but instead of using an index to access the attribute values you use a key. The key does not have to be an integer like the list index. It can be an integer or a string or any "hashable" Python type.
bob = {"Character":"Mage", "Health":7, "Stamina":3, "Magic":17} jan = {"Character":"Thief", "Health":11, "Stamina":17, "Magic":2} bob['Health'] = max(0, bob['Health'] - random.randint(1,4))Menator mentions using something called a class, and I think that is a really good fit for this problem. Unlike lists or dictionaries, a class is not a built-in Python data type, it is a user defined data type. You can define your own classes to represent your data in ways that are convenient to you.
class Character(): def __init__(self, name, type_, health=0, stamina=0, magic=0): self.name = name self.type = type_ self.health = health self.stamina = stamina self.magic = magic def is_alive(self): return self.health > 0 def who_are_you(self): print('I am', self.name) def look_out(self, d): self.health = max(0, self.health - random.randint(1, d)) return self.is_alive() def print_status(self): print(self.name, 'is', 'alive' if self.is_alive() else 'very unwell') characters = { 'Bob':Character('Bob', 'Mage', 7, 3, 17), 'Jan':Character('Jan', 'Thief', 11, 17, 2) } its_all_fun = True while its_all_fun: for character in characters.values(): if not character.look_out(4): character.print_status() its_all_fun = False breakIn addition to data, classes also have methods; functions that are particular to the class. In many ways classes are like modules. A module has attributes that you can access and functions you execute. An object has attributes you can access and methods you can execute. You can almost think of a class as a module generator. Each time you make an instance of a class (called an object), this creates a new set of attributes that are unique to that instance/object. In the example above it is almost like we have ben.py
name = 'Ben' type_ = 'Mage' health = 7 stamina = 3 magic = 17 def is_alive(): return health > 0 def who_are_you(): print('I am', name) ...And another module jan.py
name = 'Jan' type_ = 'Thief' health =11 stamina = 17 magic = 2