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"?
#1
I'm sorry if the title confused you. This is the first time I'm posting here, forgive me if I have mistakes.

Here's what I'm trying to do:
I'm trying to assign multiple variables to a single value;

from globals import health
from globals import stamina
from globals import magicka

assassin = "Assassin"; health += 100; stamina += 140; magicka += 60
mage = "Mage"; health += 80; stamina += 60; magicka += 160
warrior = "Warrior"; health += 140; stamina += 120; magicka += 40
Health, stamina and magicka variables are in a file called globals.py, you can see it clearly. And my globals.py file is as following;

gold = 0
health = 0
stamina = 0
mana = 0
mage = "Mage" is my value here. This will not be changed. However, since health, stamina, and mana are variables, and the player will drain those attributes, these will change throughout the game. So I'm trying to assign different stat pools to different roles.
I don't get an error. When I print the role, health, stamina and magicka, the output is this;

Output:
Mage 320 320 260
These are the output values I keep getting. Doesn't matter what role I choose. The only thing changing is the role. I keep getting the same/similar health, stamina and magicka values. I don't know how can I assign multiple variables to a single value. I'm still learning Python and I'm coding a simple text RPG while learning to keep my interest at its peak levels. Sorry, it's currently 3 A.M here and I'm getting tired. I tried to roughly translate my original codes to English. So I simply rewrote the whole code. If you see any mistakes, there are none in my original files.

All help is greatly appreciated. Thanks so much in advance!
Reply
#2
I would look into making a character class and check out python dicts.

Maybe use something like this:

#! /usr/bin/env python3

class Character:
    def __init__(self):
        self.profession = None
        self.gold = 0
        self.health = 0
        self.mana = 0

    def traits(self):
        return {'Profession': self.profession,
                'Gold': self.gold, 'Health': self.health, 'Mana': self.mana}


mark = Character()
mark.profession = 'assassin'
mark.gold = 100
mark.health = 100
mark.mana = 0

bob = Character()
bob.profession = 'mage'
bob.gold = 25
bob.health = 85
bob.mana = 125

for trait, level in mark.traits().items():
    print(f'{trait}: {level}')
print()
for trait, level in bob.traits().items():
    print(f'{trait}: {level}')
Output:
Profession: assassin Gold: 100 Health: 100 Mana: 0 Profession: mage Gold: 25 Health: 85 Mana: 125
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#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


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