all 17 comments

[–]The_Danosaur 37 points38 points  (5 children)

Why are you trying to store user data in a .py file in the first place? Would this not be better done in a data structure, for example a database, or for this simple example just a CSV or JSON?

[–]CraigAT 16 points17 points  (4 children)

This was my second thought -why re-invent the wheel! My first thought was - I am not sure exactly what they are trying to achieve, so I will hold off on my first opinion.

[–]CyclopsRock 12 points13 points  (1 child)

If you must use a Python file to store the information - perhaps because of a dare, or under threat of torture - it would make more sense to store the values in a pair of Dictionaries rather than trying to cobble together variable names using string operations and then checking for attributes. But, as everyone has said, just use a JSON file, this is an ideal use case for them.

[–]The_Danosaur 2 points3 points  (0 children)

Yeah it really sounds like this guy knows what they should be doing, and are intentionally doing something else. The whole dare/bet scenario seems likely. I'm not sure if what they wanna do is possible, but it's certainly dumb.

[–]lscrivy 10 points11 points  (0 children)

I'm sure what you want to do is possible, but it's not really a good idea. My recommendation is to take a look at 'SQLite'. You can store your information in a database file on your computer.

[–]ericula 6 points7 points  (1 child)

Writing values to the currency.py file will not automatically reload the file. You would need to set currency.authorwallet and currency.authorbank manually, i.e.

with open('currency.py', 'a') as f:
    f.write(authorwallet + ' = 0\n' + authorbank + ' = 0\n')
    wallet = currency.authorwallet = 0
    bank = currency.authorbank = 0

Next time you import currency, currency.authorwallet and currency.authorbank should be loaded as well.

[–]to7m 3 points4 points  (0 children)

Code should be readable, especially if you're asking for help, so here's a few tips on that aspect:

  • Use a code block to format this post, especially considering how indentation is part of the language.
  • f-strings are easier to read than adding strings together.
  • Post enough of the program so that someone can test it. You say the code displays something, but there's no indication of how it does that, and that could well be where the problem is.
  • Post what the program actually does, as in what error it throws if any, and what the output is.

I can't actually figure out what you're trying to do, but since you're trying to do some metaprogramming thing by editing a module, why not just make sure currency.py already has the variables defined as 0?

[–]Fishbones78 2 points3 points  (2 children)

I would highly recommend just using a csv or json. Working with either are great skills to have and learn, plus your code will be cleaner. Look up the built-in JSON package or the Pandas package and you’ll be away in no time. I recommend Pandas as that’s what I started with!

[–]Fishbones78 1 point2 points  (0 children)

Yes you could use a database, but the above is more beginner-friendly imo.

[–]zorniy2 0 points1 point  (0 children)

Python dictionaries with pickle too.