File 第一点 num2A=int(float(inputsA[1])#将数字拉到第三个点 OperatorA=(inputsA[2])#将运算符拉到第二个位置 如果OperatorA==“+”:#函数未用作,则math()用于上一个程序的主计算 打印(“结果是”
第一点 num2A=int(float(inputsA[1])#将数字拉到第三个点 OperatorA=(inputsA[2])#将运算符拉到第二个位置 如果OperatorA==“+”:#函数未用作,则math()用于上一个程序的主计算 打印(“结果是”,self.add(num1A,num2A)) elif OperatorA==“-”: 打印(“结果是”,self.subtract(num1A,num2A)) elif OperatorA==“*”: 打印(“结果是”,self.multiply(num1A,num2A)) elif OperatorA==“/”: 打印(“结果是”,self.divide(num1A,num2A)) def allInOne(自身、n1、n2): #定义dict res={“add”:self.add(n1,n2),“sub”:self.subtract(n1,n2),“mult”:self.multiply(n1,n2),“div”:self.divide(n1,n2)} #用于匹配密钥的for循环 对于注册表项: 如果键==“添加”: 打印(n1,“+”,n2,“=”,self.add(n1,n2)) 如果键==“sub”: 打印(n1,“-”,n2,“=”,自减(n1,n2)) 如果键==“mult”: 打印(n1,“*”,n2,“=”,自乘(n1,n2)) 如果键==“div”: 打印(n1,“/”,n2,“=”,自除法(n1,n2)) 打印(res) def选项(自我、选择、stringInputB): 自我选择 self.stringInputB inputsB=stringInputB.split(“,”) self.num1B=int(float(inputsB[0])#在第一个位置提取数字 self.num2B=int(float(inputsB[1])#在第三个点拉取数字 #OperatorB=(inputsB[2])#将运算符拉到第二个位置,未使用,因此被注释掉 如果selection==“1”:#函数未用作,则math()用于上一个程序的主计算 打印(“结果是”,self.add(self.num1B,self.num2B)) elif选择==“2”: 打印(“结果是”,self.subtract(self.num1B,self.num2B)) elif选择==“3”: 打印(“结果是”,self.multiply(self.num1B,self.num2B)) elif选择==“4”: 打印(“结果是”,self.divide(self.num1B,self.num2B)) elif选择==“5”: self.scalc(self.stringInputB) elif选择==“6”: self.allInOne(self.num1B,self.num2B) def选项2(自我选择2): 自我选择2 如果selection==“1”:#函数未用作,则math()用于上一个程序的主计算 wrfile.modify() elif选择==“2”: wrfile.look() def Thankyu(self):#如果我需要多次使用,可以帮助我回忆这张照片。 打印(“\n感谢您使用我的计算器!”) 类wrfile:#用于输出到文件的类 def modify(): 原始数据=sys.stdout 将open('test3.txt','w')作为f:#这是写入test3.txt的文件 sys.stdout=f Calculator.printallcalc() 打印(“这已保存到test3.txt中!”) sys.stdout=原始值 def look(): f=open('test3.txt','r')#它读取并打印终端中的结果 打印(f.read())File 第一点 num2A=int(float(inputsA[1])#将数字拉到第三个点 OperatorA=(inputsA[2])#将运算符拉到第二个位置 如果OperatorA==“+”:#函数未用作,则math()用于上一个程序的主计算 打印(“结果是”,file,sys,File,Sys,第一点 num2A=int(float(inputsA[1])#将数字拉到第三个点 OperatorA=(inputsA[2])#将运算符拉到第二个位置 如果OperatorA==“+”:#函数未用作,则math()用于上一个程序的主计算 打印(“结果是”,self.add(num1A,num2A)) elif OperatorA==“-”: 打印(“结果是”,self.subtract(num1A,num2A)) elif OperatorA==“*”: 打印(“结果是”,self.multi
from Mylib2 import Calculator, wrfile
calc = Calculator()
calc.run() #putting my class as a variable to execute
import sys
class Calculator:
def __init__(self):
self.x = 0
self.y = 0
self.high = 0
self.low = 0
self.scalcInputs = ""
self.printall = ""
def run(self):
try:
calculate = input(
"Would you like to use the calculator? Yes or No: ")
calculate = calculate.lower()
except KeyboardInterrupt:
print("You have manually terminated the program.")
exit(0)
menu = ["1. Add two numbers", "2. Subtract two numbers", "3. Multiply two numbers",
"4. Divide two numbers", "5. Scalc", "6. All In One"]
menu2 = ["1. Save", "2. Read"]
try:
while calculate == "yes":
# Basic inputs
# float was added for non whole numbers
self.low = int(float(input("Please enter your Lower range: ")))
self.high = int(
float(input("Please enter your Higher range: ")))
self.x = int(float(input("Please enter your first number: ")))
self.y = int(float(input("Please enter your second number: ")))
# String Input
# Changed back after feedback
self.stringInputA = input(
"Please a math equation you would like solved as (10,10,+): ")
# Menu Input
for s in menu:
print(s)
self.selection = input("Please select an operation from the menu: ")
# Input for n1 and n2 "10 10 +"
self.stringInputB = input(
"Please enter two numbers to calculate and an operator for Scalc (10,10,+): ")
# Menu2 Input to save or read
for n in menu2:
print(n)
self.selection2 = input("Would you like to save or read your calculations? ")
if self.selection2 == 1:
wrfile.modify()
elif self.selection2 == 2:
wrfile.look()
# Prints out results
self.math()
self.scalc(self.stringInputA)
self.option(self.selection, self.stringInputB)
self.thankyou()
# menu condition for saving or reading
# old position
# self.printallcalc(self.selection, self.stringInputA, self.stringInputB)
# self.printall = (self.math(), self.scalc(self.stringInputA), self.option(self.selection, self.stringInputA))
# Loop
calculate = input("Would you like to calculate again? Yes or No: ")
calculate = calculate.lower()
# break #trying to break the loop get inputs and then continue
# if self.selection2 == 1:
# wrfile.modify()
# elif self.selection2 == 2:
# wrfile.look()
# elif calculate == "yes":
# run()
# if statement was added, so that variations of no could be caught
if calculate == "no":
print("Have a great day!")
else:
print("Please enter only Yes or No. Have a great day!")
# Please enter only yes or no added to help the user understand other inputs were not accepted
except KeyboardInterrupt:
print("You have manually terminated the program.")
exit(0)
def printallcalc(): #put all prints to here so that wrfile could call
self.scalc(self.stringInputA)
self.option(self.selection, self.stringInputB)
self.thankyou()
def add(self, x: float, y: float):
return x + y
# Function line: subtracts two numbers
def subtract(self, x: float, y: float):
return x - y
# Function line: multiplies two numbers-
def multiply(self, x: float, y: float):
return x * y
# Function line: divides two numbers
def divide(self, x: float, y: float):
try:
return x / y
except ZeroDivisionError:
return "Undefined"
def math(self): # My function to define the results of the calcuation and how to print it
# math(x, y) initially had each math function on it's own, but I combined it in week 3.
print("The result of ", self.x, "+", self.y, "=", self.add(self.x, self.y))
print("The result of ", self.x, "-",
self.y, "=", self.subtract(self.x, self.y))
print("The result of ", self.x, "*",
self.y, "=", self.multiply(self.x, self.y))
try:
return print("The result of ", self.x, "/", self.y, "=", self.divide(self.x, self.y))
# Telling program, I know, thank you and print this in it's place.
except ZeroDivisionError:
print("The result of ", self.x, "/", self.y,
"=", self.divide(self.x, self.y))
# Changed back after feedback.
def is_inrange(self, low: float, high: float):
if low > self.x | self.y and high < self.x | self.y:
print("The input values are outside the input ranges."
"\nPlease check the numbers and try again"
"\nThank you for using my calculator")
# This funciton does the math for my scalc. I did left my math(x,y) so it could compute the
def scalc(self, stringInputA: str):
# rest of the code. I did not want to implement another 4 functions, so I added if - elif to calculate
# the string.
inputsA = stringInputA.split(",")
num1A = int(float(inputsA[0])) # pull the number in the first spot
num2A = int(float(inputsA[1])) # pull the number in the thrid spot
OperatorA = (inputsA[2]) # pulls the operator in the second position
if OperatorA == "+": # function was not used as, math() was used for the main calculation for the previous program
print("The result is", self.add(num1A, num2A))
elif OperatorA == "-":
print("The result is", self.subtract(num1A, num2A))
elif OperatorA == "*":
print("The result is", self.multiply(num1A, num2A))
elif OperatorA == "/":
print("The result is", self.divide(num1A, num2A))
def allInOne(self, n1, n2):
# defining dict
res = {"add": self.add(n1, n2), "sub": self.subtract(n1, n2),"mult": self.multiply(n1, n2), "div": self.divide(n1, n2)}
# for loop for matching keys
for key in res:
if key == "add":
print(n1, "+", n2, "=", self.add(n1, n2))
if key == "sub":
print(n1, "-", n2, "=", self.subtract(n1, n2))
if key == "mult":
print(n1, "*", n2, "=", self.multiply(n1, n2))
if key == "div":
print(n1, "/", n2, "=", self.divide(n1, n2))
print(res)
def option(self, selection, stringInputB):
self.selection
self.stringInputB
inputsB = stringInputB.split(",")
self.num1B = int(float(inputsB[0])) # pull the number in the first spot
self.num2B = int(float(inputsB[1])) # pull the number in the thrid spot
#OperatorB = (inputsB[2]) # pulls the operator in the second position, not used so commented out
if selection == "1": #function was not used as, math() was used for the main calculation for the previous program
print("The result is", self.add(self.num1B, self.num2B))
elif selection == "2":
print("The result is", self.subtract(self.num1B, self.num2B))
elif selection == "3":
print("The result is", self.multiply(self.num1B, self.num2B))
elif selection == "4":
print("The result is", self.divide(self.num1B, self.num2B))
elif selection == "5":
self.scalc(self.stringInputB)
elif selection == "6":
self.allInOne(self.num1B, self.num2B)
def option2(self, selection2):
self.selection2
if selection == "1": #function was not used as, math() was used for the main calculation for the previous program
wrfile.modify()
elif selection == "2":
wrfile.look()
def thankyou(self): # Helps me recall this print if I need to use it multiple times.
print("\nThank you for using my calculator!")
class wrfile: #class for output to file
def modify():
original_stdout = sys.stdout
with open('test3.txt', 'w') as f: #this is writes out file to test3.txt
sys.stdout = f
Calculator.printallcalc()
print("This was saved into test3.txt!")
sys.stdout = original_stdout
def look():
f = open('test3.txt', 'r') #this reads and prints results in terminal
print(f.read())