Question: Having trouble getting selection 7 and 8 to write/ read files. Can anyone see what's wrong? They are performing math functions not write/read functions #code
Having trouble getting selection 7 and 8 to write/ read files. Can anyone see what's wrong? They are performing math functions not write/read functions
#code
from mylib import mylib
def IsInRange(low_range,high_range,number):
if number>=low_range and number<=high_range:
return True
else:
return False
def Menu():
print("Select Operation.")
print("1.Addition")
print("2.Subtraction")
print("3.Multiply")
print("4.Divide")
print("5.scalc")
print("6.All in one")
print("7.Write to File")
print("8.Read from the file")
print(" ")
def main():
obj = mylib()
Menu()
choice = input("Enter choice(1/2/3/4/5/6/7/8 or q):")
try:
low_range = int(input("lower range variable"))
high_range= int(input("higher range variable"))
var1 = int(input("first variable"))
while not IsInRange(low_range, high_range, var1):
var1=int(input("Re-Enter first variable"))
var2=int(input("Enter your second variable"))
while not IsInRange(low_range, high_range, var2):
var2=int(input("Re-Enter your second variable in the range"))
except ValueError:
print('please use integers')
return
operation = str(input("Enter problem string like this, N1,N2,and Operator, "))
try:
if choice == '1':
print("the sum of", var1, "+", var2, "=", obj.addition(var1, var2))
elif choice == '2':
print("the difference of:", var1, "-", var2, "=", obj.subtraction(var1, var2))
elif choice == '3':
print("the product of:", var1, "*", var2, "=", obj.multiplication(var1, var2))
elif choice == '4':
print("the quotient of:", var1, "/", var2, "=", obj.division(var1, var2))
elif choice == '5':
print(obj.scalc(operation))
elif choice == '6':
print(obj.allInOne(var1,var2))
elif var1 >= low_range and var2 <= high_range:
print("the sum of", var1, "+", var2, "=", obj.addition(var1, var2))
print("the difference of", var1, "-", var2, "=", obj.subtraction(var1, var2))
print("the product of", var1, "*", var2, "=", obj.multiplication(var1, var2))
print("the quotient of", var1, "/", var2, "=", obj.division(var1, var2))
print(obj.scalc(operation))
print(obj.allInOne(var1,var2))
elif choice == 7:
wrFile = wrfile()
wrFile.write(self.allInOne())
elif choice == 8:
wrFile = wrfile()
wrFile.read()
else:
print(IsInRange(low_range, high_range,int))
except ZeroDivisionError:
print("You cannot divide by zero!!")
restart = input("Would like to try again? y for yes!")
if restart == "y" :
main()
else:
print("Thank you for using the calculator.")
return
main()
#mylib
class mylib:
def addition(self, var1, var2):
return var1 + var2
def subtraction(self, var1, var2):
return var1 - var2
def division(self, var1, var2):
if var1 == 0 or var2 == 0:
raise ZeroDivisionError
return var1 / var2
def multiplication(self, var1, var2):
return var1 * var2
def scalc(self, p1):
istring = p1.split(",")
res = 0
if istring[2] == "+":
res = self.addition(int(istring[0]), int(istring[1]))
elif istring[2] == "-":
res = self.subtraction(int(istring[0]), int(istring[1]))
elif istring[2] == "*":
res = self.multiplication(int(istring[0]), int(istring[1]))
elif istring[2] == "/":
res = self.division(int(istring[0]), int(istring[1]))
return res
def allInOne(self, var1, var2):
result1 = self.addition(var1, var2)
print("{0}+{1}={2}".format(var1, var2, result1))
result2 = self.subtraction(var1, var2)
print("{0}-{1}={2}".format(var1, var2, result2))
result3 = self.multiplication(var1, var2)
print("{0}*{1}={2}".format(var1, var2, result3))
result4 = self.division(var1, var2)
print("{0}/{1}={2}".format(var1, var2, result4))
return {
"addition": result1,
"subtraction": result2,
"multiplication": result3,
"division": result4,
}
class wrfile():
def __init__(self):
self.filename = 'output.txt'
def read(self):
try:
file = open(self.filename, 'r')
except:
print('File does not exist')
return
print(file.read())
def write(self, output):
file = open(self.filename, 'a')
file.write(str(output) + " ")
print('Output Written to the file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
