Python Forum
How can I assign "multiple variables" to a single "value"?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I assign "multiple variables" to a single "value"?
#3
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:
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
            break
In 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
Reply


Messages In This Thread
RE: How can I assign "multiple variables" to a single "value"? - by deanhystad - Oct-04-2021, 03:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 981 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Create multiple/single csv file for each sql records mg24 6 1,420 Sep-29-2022, 08:06 AM
Last Post: buran
  Reshaping a single column in to multiple column using Python sahar 7 2,083 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,751 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Split single column to multiple columns SriRajesh 1 1,343 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,327 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  Ploting single column with multiple category drunkenneo 1 1,995 May-26-2021, 04:51 PM
Last Post: jefsummers
  Inserting multiple rows in a single request. swaroop 2 2,917 Jan-11-2021, 01:34 PM
Last Post: swaroop
  Fetching data from multiple tables in a single request. swaroop 0 1,907 Jan-09-2021, 04:23 PM
Last Post: swaroop
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,358 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020