Reputation: 11
I am quite confused about what is wrong with my code. I am trying to make a math practice program. However, when I choose an operation, no matter what I type in, it will always take me to addition. Can someone please help me solve this problem. My little brother is not very excitedly waiting. :)
from random import randint
print("Welcome to Luke's Math Practice")
score = 0
def mathAddition():
global score
numberOne = randint(0, 100)
numberTwo = randint(0, 100)
answer = input(f"{numberOne} + {numberTwo} = ")
correctAnswer = numberOne + numberTwo
if int(answer) == correctAnswer:
print("Correct!")
score = score + 1
elif int(answer) != correctAnswer:
print("Incorrect!")
print("The Correct Answer was", correctAnswer)
print("Your Score Is:", score)
choiceOperation()
else:
print("Sorry That Was an Invalid Answer")
def mathSubtraction():
global score
numberOne = randint(0, 100)
numberTwo = randint(0, 100)
answer = input(f"{numberOne} - {numberTwo} = ")
correctAnswer = numberOne - numberTwo
if int(answer) == correctAnswer:
print("Correct!")
score = score + 1
elif int(answer) != correctAnswer:
print("Incorrect!")
print("The Correct Answer was", correctAnswer)
print("Your Score Is:", score)
choiceOperation()
else:
print("Sorry That Was an Invalid Answer")
def mathMultiplication():
global score
numberOne = randint(0, 20)
numberTwo = randint(0, 20)
answer = input(f"{numberOne} * {numberTwo} = ")
correctAnswer = numberOne * numberTwo
if int(answer) == correctAnswer:
print("Correct!")
score = score + 1
elif int(answer) != correctAnswer:
print("Incorrect!")
print("The Correct Answer was", correctAnswer)
print("Your Score Is:", score)
choiceOperation()
else:
print("Sorry That Was an Invalid Answer")
def mathDivision():
global score
numberOne = randint(0, 20)
numberTwo = randint(0, 20)
answer = input(f"{numberOne} / {numberTwo} = ")
correctAnswer = numberOne / numberTwo
if int(answer) == correctAnswer:
print("Correct!")
score = score + 1
elif int(answer) != correctAnswer:
print("Incorrect!")
print("The Correct Answer was", correctAnswer)
print("Your Score Is:", score)
choiceOperation()
else:
print("Sorry That Was an Invalid Answer")
def choiceOperation():
operationChoice = input("Would you like to practice adding, subtracting, multiplying, or dividing? ")
if operationChoice == "add" or "+" or "adding" or "addition":
while True:
mathAddition()
elif operationChoice == "subtract" or "-" or "subtracting" or "subtraction":
while True:
mathSubtraction()
elif operationChoice == "multiply" or "*" or "x" or "multipying" or "multiplication":
while True:
mathMultiplication()
elif operationChoice == "divide" or "/" or "dividing" or "division":
while True:
mathDivision()
else:
print("Sorry, That Was an Invalid Choice")
choiceOperation()
Upvotes: 0
Views: 385
Reputation: 1005
Use this and refer to here.
def choiceOperation():
operationChoice = input("Would you like to practice adding, subtracting, multiplying, or dividing? ")
if operationChoice in ['add', '+', 'adding', 'addition']:
mathAddition()
elif operationChoice in ['subtract', '-', 'subtracting', 'subtraction']:
mathSubtraction()
elif operationChoice in ['multiply', '*', 'x', 'multipying', 'multiplication']:
mathMultiplication()
elif operationChoice in ['divide', '/', 'dividing', 'division']:
mathDivision()
else:
print("Sorry, That Was an Invalid Choice")
Upvotes: 1
Reputation: 9256
This line
if operationChoice == "add" or "+" or "adding" or "addition":
is not doing what you think it is doing.
It is checking if operationChoice == "add"
or if any of "+" or "adding" or "addition"
evaluate to true. (All non-empty strings evaluate to True)
Either make it
if operationChoice in ("add", "+", "adding", "addition"):
OR
if operationChoice == "add" or operationChoice == "+" or operationChoice == "adding" or operationChoice == "addition":
Upvotes: 1
Reputation: 2255
Here's the problem, in your choinceOperation funtion. the if statemnts should looks like:
if operationChoice in ("add", "+", "adding", "addition"):
The reason why your codes always leads to "+" is that:
operationChoice == "add" or "+" or "adding" or "addition"
# (operationChoice == "add") or "+" or "adding" or "addition"
# and the first fails, but the "+" will always be True.
Upvotes: 1