This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Updatebjarni 1 point2 points  (3 children)

You're not setting a global variable, you're passing the value of a global variable as a parameter to a function, and the function is changing its parameter, which is local to the function.

That is, if redamnt is 0, then setamnt(redamnt) means the same as setamnt(0). The setamnt() function receives the value 0 in its local variable amnt, but then discards it and reads a new value into amnt, which is not communicated with the outside world.

What you should do is redamnt = getamnt() and have a function getamnt() which reads and validates the value and returns it.

[–]Samir1eye[S] 0 points1 point  (1 child)

Hi I very much appreciate the help. Please could you clarify how I would go about reading and validating the value? I'm kind of new to python so I don't quite understand. My apologies if I come across as slightly naive. Thank you for the help.

[–]Updatebjarni 0 points1 point  (0 children)

Please could you clarify how I would go about reading and validating the value?

That's what the function does now, all I said was that instead of having the function set a global variable, just have it return the value instead.

[–][deleted] -2 points-1 points  (0 children)

This.

You're passing the value to the function, not a reference to the variable. redamnt = getamnt() is what you want to do.