Search code examples
pythonpython-3.xclassglobal-variables

Changing the value of variables in a class through a function outside the class in python


I am trying to modify the value of a variable (in this case an integer) that is located inside of a class using a function that's outside of that class. But when I call the function, the value of the variable us_balance stays the same: 42.

class Country:
    us_balance = 42

def buy(country, amount):
    if country.lower == "us":
        Country.us_balance = Country.us_balance - amount

def stats():
    print(Country.us_balance)

while True:
    user_input = input("> ").lower()

    if user_input == "buy":
        amount = input("$ ").lower()
        balance_to_set = amount.split()
        buy(balance_to_set[0], int(balance_to_set[1]))
    if user_input == "stats":
        stats()
        break

Any ideas on how to fix this?


Solution

  • All I had to do was just add parentheses to the .upper() function in the class