I made a calculator for a class I am taking. It works like this:
- Tells user the input range is between two numbers, and asks if want to change the range. If yes, asks the user input new numbers (if they input anything that isn't a number, we catch it with exception handling). If no, proceeds and ask the user for two numbers, and performs addition, subtraction, multiplication, and division of the numbers. After the calculations it ask the user if they want to continue using the calculator. Depending on the answer, the calculator restarts or the application stops.
TL;DR: Everything works correctly but I am asking for suggestions to improve my code. I believe its excessively long.
#---------------------------------------
My Calculator
#---------------------------------------
def AddNum(FN, SN):
print("\nThe Addition result is\t", FN, "+", SN, "\t=", (FN + SN))
def SubNum(FN, SN):
print("\nThe Subtraction result is\t", FN, "-", SN, "\t=", (FN - SN))
def MulNum(FN, SN):
print("\nThe Multiplication result is\t", FN, "*", SN, "\t=", (FN * SN))
def DivNum(FN, SN):
if FN == 0 or SN == 0:
print("\nThe Division result is\t\t\t", FN, "/", SN, "\t=",
"You cannot divide by Zero")
elif FN != 0 or SN != 0:
print("\nThe Division result is\t", FN, "/", SN, "\t=",
(FN / SN))
def IsInRange(LR, HR, FN, SN):
if LR <= FN <= HR and LR <= SN <= HR:
return
else:
print("\n[The values are outside the input ranges.] \n\nPlease check the numbers and try again")
myLoop()
print("""------------------------------------------------------
\nWelcome to my Calculator.
\nGive me two numbers and I will calculate them for you.
------------------------------------------------------""")
def myLoop():
Loop4Ever = "Y"
ChngRng = ""
FN, SN = 0, 0
while 1:
if Loop4Ever == "Y" or "y":
LR = -100
HR = 100
print("\n{--- My current input range is from", LR, "to", HR, "for each of the two numbers. ---}")
while 1:
try:
CRlist = ["Y", "y", "N", "n"]
ChngRng = input("\nWould you like to change the input range numbers (Y or N)?")
if ChngRng in CRlist:
break
else:
print("\nIncorrect input, only use Y or N.")
except ValueError:
continue
if ChngRng == "Y" or ChngRng == "y":
while 1:
try:
LR = float(input("\nSet new Lower Range?\t"))
break
except ValueError:
print("Incorrect input, only enter numbers.")
continue
while 1:
try:
HR = float(input("\nSet new Higher Range?\t"))
break
except ValueError:
print("Incorrect input, only enter numbers.")
continue
elif ChngRng == "N" or ChngRng == "n":
pass
print("\nHigher Range--->", HR)
print("\nLower Range--->", LR)
while 1:
try:
FN = int(input("\nFirst number?\t"))
break
except ValueError:
print("\nIncorrect input, only enter numbers.")
continue
while 1:
try: #Try block to catch and handle incorrect user input.
SN = int(input("\nSecond number?\t"))
break
except ValueError:
print("\nIncorrect input, only enter numbers.")
continue
IsInRange(LR, HR, FN, SN)
AddNum(FN, SN)
SubNum(FN, SN)
MulNum(FN, SN)
DivNum(FN, SN)
Loop4Ever = "0"
LpList = ["Y", "y", "N", "n"]
while 1:
try:
Loop4Ever = str(input("\nContinue using the Simple Calculator (Y or N)? "))
if Loop4Ever not in LpList:
print("\nIncorrect input, only use Y or N.")
continue
except ValueError:
print("\nIncorrect input, only use Y or N.")
continue
else: #If user input not in our list.
if Loop4Ever == "Y" or Loop4Ever == "y":
while 1:
try:
myLoop()
break
except ValueError:
continue
elif Loop4Ever == "N" or Loop4Ever == "n":
print("\nThanks for using our calculator.")
exit()
myLoop() #Initiate Calculator.