1

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()
  • If ```tryAgain not in yesList or tryAgain not in noList``` in your code, ```noList``` is non-empty so it has ```True``` value. That is why –  Jul 21 '21 at 17:09
  • I replaced tryAgain not in yesList or noList with tryAgain not in yesList or not in noList, but it didn't change anything. Anything else I could try? – Bryan Chavez Jul 21 '21 at 17:14
  • I am telling to try ```tryAgain not in yesList or tryAgain not in noList``` –  Jul 21 '21 at 17:15
  • Yeah I tried that, same result. I accidently sent "I replaced tryAgain not in yesList or noList" because I thought I could make a new line with the return key on my macbook. – Bryan Chavez Jul 21 '21 at 17:19

0 Answers0