Tuesday, January 30, 2018

python 3.x - Python3: If elif else with while loops in a function

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()

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...