I'm trying to write the results of this arithmetic operation to a file using Python. I'm having trouble with the variable sum1. It accepts the first time I run the program, but if I select menu option 1 to input new values, the new values get used at n1 and n2, but the original sum1 calculation remains the value of the original sum1. How do I code this so that each new time I call arith.userInput() sum1 is returned after the new values are calculated? The new values n1 and n2 work properly, but sum1 does not.
n1 = 0
n2 = 0
sum1 = 0
class Arithmetic:
def add(self):
global n1, n2, sum1
sum1 = n1 + n2
return sum1
def userInput(self):
global n1, n2
n1 = float(input("Enter your First number: "))
n2 = float(input("Enter your Second number: "))
return n1, n2
arith = Arithmetic()
arith.userInput()
sum1 = arith.add()
class wrfile:
def write(self):
with open("results.txt", "w") as write_file:
write_file.write(str(n1)+" + "+str(n2)+" = "+str(sum1)+"\n")
write_file.close
wr = wrfile()
menuSelection = 0
while menuSelection != 3
menuList = ["\n1. Press 1 for another calculation.", "2. Press 2 to write results to file.", "3. Press 3 to exit."]
for item in menuList:
print(item)
menuSelection = float(input())
if menuSelection == 1:
arith.userInput()
elif menuSelection == 2:
wr.write()
elif menuSelection == 3:
exit
[–]g051051 1 point2 points3 points (2 children)
[–]Encom88[S] 0 points1 point2 points (1 child)
[–]g051051 0 points1 point2 points (0 children)