Yesterday I started to learn Python, and so far I'm enjoying the simplicity of it. Today, I tried creating a simple calculator that can add, subtract, multiply, divide, and mod. While it works perfectly fine, whenever the program asks the user if they wish to try again after their second attempt, the line "What? Try again." appears, which should only appear if the user gives an answers that is not in noList or yesList to the question "Try again?". Sorry if none of this makes sense, but I'll put my code below.
functionList = ["add", "subtract", "multiply", "divide", "mod"]
yesList = ["yes", "yeah", "yup", "y"]
noList = ["no", "nope", "nah", "n"]
print("Welcome to Bryan's Calculator!")
def calculator():
whatFunction = input("What math function do you want to use (add/subtract/multiply/divide/mod)? ")
if whatFunction == "add":
numAddOne = float(input("What is your first number? "))
numAddTwo = float(input("What is your second number? "))
answerAdd = numAddOne + numAddTwo
print(str(answerAdd) + " is the answer!")
if whatFunction == "subtract":
numSubtractOne = float(input("What is your first number? "))
numSubtractTwo = float(input("What is your second number? "))
answerSubtract = numSubtractOne - numSubtractTwo
print(str(answerSubtract) + " is your answer!")
if whatFunction == "multiply":
numMultiplyOne = float(input("What is your first number? "))
numMultiplyTwo = float(input("What is your second number? "))
answerMultiply = numMultiplyOne * numMultiplyTwo
print(str(answerMultiply) + " is your answer!")
if whatFunction == "divide":
numDivideOne = float(input("What is your dividend? "))
numDivideTwo = float(input("What is your divisor? "))
answerDivide = numDivideOne / numDivideTwo
print(str(answerDivide) + " is your answer!")
if whatFunction == "mod":
numModOne = float(input("What is your dividend? "))
numModTwo = float(input("What is your divisor? "))
answerMod = numModOne % numModTwo
print(str(answerMod) + " is your remainder!")
elif whatFunction not in functionList:
print("What? Try again.")
calculator()
def askTryAgain():
tryAgain = input("Try again? ")
if tryAgain in yesList:
calculator()
if tryAgain in noList:
print("Thanks for using my calculator!")
exit()
if tryAgain not in yesList or noList:
print("What? Try again")
askTryAgain()
calculator()
askTryAgain()