Is there any type of variable that allows me to increase its value, without the memorized value being overwritten? Example:
def main():
var = 0
inc = input("Want to increase your variable?")
if inc == "yes":
var = var + 1
main()
#restart the script from the top
if inc == "no":
exit()
When the code restarts, the var will return to its value of “0” without having remembered that “var = var + 1” bit we did, is there any variable type that will update itself to match the changes made in the script?
When you run your function, main(), you’re resetting var back to 0. Notice how it’s inside the function, and so whatever is inside a function will be run.
Here var won’t be changed to 0 every time:
var in this case is a global variable. It can be accessed throughout the whole code.
The code above, when run:
I hope this answered your problems. If you have any other questions, I’ll try answer them ;). I’m a bit of a novice to python.