1

I want to change the value of a variable that's inside another variable, this is the code I have so far:

person1tokens = 0
person2tokens = 0
token_price = 5
accounts = ["person1tokens","person2tokens"]

def add_tokens(person,amount):
    if person in accounts:
        cost = amount*token_price
        print("You need to pay %d$" % (cost))
        payment = int(input("Payment? "))
        if payment != cost:
            print("Please enter the right amount next time, nothing changed")
        else:
            --Help here--, change value of a the in the function specified account to <account> += amount
            print("Added " + str(amount) + " tokens to account " + str(person) + ", your current balance is " + str(eval(person)"."))
    else:
        print("fail")

I basically want to change the value of person1tokens(for example) without knowing beforehand that I want to change that value, and I want to change that value where it says --Help here--. But the problem is that it is inside the person variable and I don't know how to get the program to "unpack" it from there. So for example if the user does as follows:

add_tokens("person1tokens",5)
You need to pay 25$
Payment? 25
Added 25 tokens to account person1tokens, your current balance is 25.

Any ideas?

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
ErikN118
  • 17
  • 1
  • 7
  • You're essentially trying to access a variable by its string name, you can find information on how to do that here https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string – sedavidw Dec 29 '20 at 01:08
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – mkrieger1 Dec 29 '20 at 01:27
  • This doesn't really make sense. Why not just store the integer values for the token counts, directly in the `accounts` list? – Karl Knechtel Jul 05 '22 at 01:00

1 Answers1

1

If I'm understanding you correctly here, you want to be able to change the value of either person1tokens or person2tokens without knowing which will be changed beforehand (please correct me if I'm wrong). One way I can think to do this would be using a dictionary rather than storing their values in their own separate variables, and then changing the values based off of that.

accounts = {'person1tokens': 0,
            'person2tokens': 0}

def add_tokens(person, amount):
    if person in accounts:
        cost = amount*5
        payment = int(input("Payment? "))
        if payment != cost:
            print("Please enter the right amount next time, nothing changed")
        else:
            accounts[person] = accounts[person] + cost
            print("Added " + str(amount) + " tokens to account " + str(person) + ", your current balance is " + str(accounts[person]) + ".")
    else:
        return False

This would allow you to have a large amount of people and simply refer to them by name. The one downside to this for you is that you won't be able to refer to the variables, but you can still reference the persons balance by doing accounts['nameofperson']

Hope this helped!

Nether Man
  • 70
  • 1
  • 10
  • Thanks, you understood correctly and it worked! I'm still perplexed by how fast questions get answered by the StackOverflow community. – ErikN118 Dec 29 '20 at 01:46
  • @ErikN118 haha, oftentimes they just get sent to a page we can see that recommends them to us. I will warn you, however, not to ask too many questions off the bat: I got my account locked a long time ago because of that. The best thing you can do to learn is spend a lot of time researching before you ask. – Nether Man Dec 29 '20 at 03:12