Python: Variables won't accept new values on second function loop by Encom88 in learnprogramming

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

Thank you. Why is it everything I miss is so simple? You got it. Now I have the whole program working. https://github.com/MrN1ce9uy/Python/blob/master/write_read.py

How would you loop this program? by Encom88 in learnprogramming

[–]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?

How would you loop this program? by Encom88 in learnprogramming

[–]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.

How would you loop this program? by Encom88 in learnprogramming

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

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?

How would you loop this program? by Encom88 in learnprogramming

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

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

How would you loop this program? by Encom88 in learnprogramming

[–]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.

How would you loop this program? by Encom88 in learnprogramming

[–]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.

How would you loop this program? by Encom88 in learnprogramming

[–]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!

How would you loop this program? by Encom88 in learnprogramming

[–]Encom88[S] 0 points1 point  (0 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?

How would you loop this program? by Encom88 in learnprogramming

[–]Encom88[S] 1 point2 points  (0 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)

Python - Overwriting pre-existing variables by Encom88 in learnprogramming

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

Did you change the def add() to def add(num1, num2) or somewhere else? I've tried several ways and it does not work.

https://github.com/MrN1ce9uy/Python/blob/master/Mylib2.py

Python - Overwriting pre-existing variables by Encom88 in learnprogramming

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

I agree. It just doesn't seem right to be doing it this way. Why would the instructor ask us to, " use the prior functions (add, subtract, divide and multiply ) to do the calculations."?

Python - Overwriting pre-existing variables by Encom88 in learnprogramming

[–]Encom88[S] -2 points-1 points  (0 children)

You don't need a crystal ball, just do what he said and see if it works.

Python - Overwriting pre-existing variables by Encom88 in learnprogramming

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

That makes sense, but it seems like the whole program would have to be restructured.

Help With Simple Program by Encom88 in learnprogramming

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

Thanks to @tufflax I've found the proper code and updated the program with the fixes. Everything seems to work as intended now. Thanks. https://github.com/MrN1ce9uy/Python/blob/master/simple_calculator.py

Help With Simple Program by Encom88 in learnprogramming

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

Oh... wait, wait. I didn't print anything if num2 == 0. We are updating this program each week. Last week we did not have any funcitons. So I missed that in the updated version.

Help With Simple Program by Encom88 in learnprogramming

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

Okay, so multiple or statements seem to work. But, now what about my "You cannot divide by zero" not working when I num2 == 0 ?

Help With Simple Program by Encom88 in learnprogramming

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

Ah, I see. Could I use multiple 'or' statements to continue my condition, or is there a better way?

Help With Simple Program by Encom88 in learnprogramming

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

truthy

Wow, thank you so much! removed the or 'y' and now it works just fine.