Search code examples
pythonlistvariables

Edit a variable from list (Cannot use dictionary)


a = 10
b = 20
ls = [a, b]

How do I change the variable a to e.g. 20 using ls[0] as a parameter?

I remember there is some way to write code where you could input string and variables and it would turn into a line of code and run it.

This is how I faintly remember it, something like:

run(ls[0], "= 20")

Showing the whole code, instead of a condensed version, what I am trying to do is load and save variables from a text file. (Code is for a terminal game about making drugs.)

money = 11000

growtime = 45
growreward = 50
gramsweed = 0
growing = False

pots = 1
seeds = 0

cooktime = 90
cookreward = 20
gramsmeth = 0
cooking = False

tanks = 0
methlitres = 0

weedselling = 0
methselling = 0
currentlyselling = False

dealers = 1
dealtime = 120
dealamount = 15

stats = [money, growtime, growreward, gramsweed, growing, pots, seeds, cooktime,
         cookreward, gramsmeth, cooking, tanks, methlitres, weedselling, methselling,
         currentlyselling, dealers, dealtime, dealamount]

boolstats = [growing, cooking, currentlyselling]

def save():
    f = open("save.txt", "w")
    for stat in stats:
        f.write(str(stat) + "\n")
    f.close()
    mainmenu()

def load():
    i = 0
    f = open("save.txt", "r")
    for stat in stats:
        print(stat)
        stats[i] = f.readline(i)
        i += 1
    j = 0
    for stat in boolstats:
        if stat == "False": boolstats[j] = False
        else: boolstats[j] = True
        j += 1
    f.close()
    mainmenu()

Solution

  • Here's an example of how to store your game data in a dict and use the json module to easily save/load it. I'm going to start by just putting all of your variable declarations into a dict (note that I would normally use the {} syntax instead of the dict() constructor, but I'm copying and pasting your existing code and this makes it slightly easier):

    stats = dict(
        money = 11000,
    
        growtime = 45,
        growreward = 50,
        gramsweed = 0,
        growing = False,
    
        pots = 1,
        seeds = 0,
    
        cooktime = 90,
        cookreward = 20,
        gramsmeth = 0,
        cooking = False,
    
        tanks = 0,
        methlitres = 0,
    
        weedselling = 0,
        methselling = 0,
        currentlyselling = False,
    
        dealers = 1,
        dealtime = 120,
        dealamount = 15,
    )
    

    and now I can write your save and load functions in just a couple lines of code each:

    import json
    
    def save():
        with open("save.txt", "w") as f:
            json.dump(stats, f)
    
    def load():
        with open("save.txt") as f:
            stats.update(json.load(f))
    

    The json module takes care of reading the lines, parsing them, converting them to the right types, all of it, because it can just pull all the information it needs right out of the dictionary object. You can't get that same type of convenience and flexibility if you have a dozen different individual variables.

    To suggest the ways you'd convert the other pieces of your game code to use a dict instead of individual variables I'd need to see that code, but hopefully this one example helps convince you that life can be much easier if you don't need to deal with variables one at a time!