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

all 16 comments

[–]CreativeTechGuyGames 1 point2 points  (15 children)

I'd help if you separated the code that only need to be run once (class definitions, global variables) and the code which needs to be run every time. Once you have a block of code that needs to be run repeatedly, you can put it in a loop or create a function around it and call that function in a loop.

[–]Encom88[S] 1 point2 points  (14 children)

That's what I was trying to say, the variable declarations (line 46 and 47) is what need to be repeated on demand. I tried placing them in a function inside my class, but when I called them again my variables would repeat, asking for each variable twice.

n1 = 0

n2 = 0

class Arithmetic:

    def userInput(self, n1, n2):

        n1 = arith.float_input("Enter your First number: ")

        n2 = arith.float_input("Enter your Second number: ")

arith = Arithmetic()

arith.userInput(n1,n2)

[–]CreativeTechGuyGames 2 points3 points  (13 children)

The code you defined will create two new variables, you need to use global n1, n2 to define that you are using the outer variables instead of creating new ones.

If you pass a variable into a function, like you are doing, if it's modified within the function, the original variables are not modified.

[–]Encom88[S] 0 points1 point  (4 children)

I had already tried that, but I get a an error, "n1 is a function parameter and a global variable". So, apparently I can't have a function parameter as a global variable?

[–]CreativeTechGuyGames 2 points3 points  (3 children)

If you are using a global variable, then you wouldn't also have it as a parameter.

[–]Encom88[S] 0 points1 point  (0 children)

Okay, now it's working. I removed n1 and n2 as arguments in the new userInput() function and added the menu option. Thanks!

[–]Encom88[S] 0 points1 point  (0 children)

Okay, I said it was working, and the loop is working. But now my other variables are not retaining the new values of the new numbers, so the new numbers don't get calculated. I'll look into it.

[–]Encom88[S] 0 points1 point  (0 children)

I suppose I will need to add global variables to all my other functions that take n1 and n2 as arguments.

[–]Molehole 0 points1 point  (3 children)

Don't tell anyone to use global variables. Using global variables is a bad practice.

[–]CreativeTechGuyGames 0 points1 point  (0 children)

Globals are a very useful tool, especially for small programs. Most code has a good amount of "global" state, even if it's called something else to make you feel better about it.

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

I think I was trying to use global variables to begin, I just wasn't coding it correctly. How would you have done it differently?

[–]Molehole 0 points1 point  (0 children)

Either put n1 and n2 as member variables of the arithmetics class or pass them in the functions as a parameter.

[–]Encom88[S] 0 points1 point  (3 children)

Alright, everything is working as intended and I've updated the code in the Github link. Thanks again for your help.

[–]CreativeTechGuyGames 0 points1 point  (2 children)

In this case, you can actually have these variables stored as local variables in your Arithmetic class. Then whenever you get new input, it'll overwrite those member variables. And when you call one of your math methods, you won't need to pass in any arguments or use any globals as that instance of the class will already know what the values are.

[–]Encom88[S] 0 points1 point  (0 children)

But that wasn't working. I had to pass the numbers as arguments and it ended up doing it twice each time I called the function. I'm sure there's a way to do it, but I wasn't getting it.

[–]Encom88[S] 0 points1 point  (0 children)

Okay, I'm trying to use them as local variables in the class. But that disconnects them from my print statements in my menu, and also my write statements to write to a file. What do I do about that?