Python3:如果在函数

时间:2017-05-14 01:42:47

标签: python-3.x function if-statement while-loop

我对我的代码有什么问题感到很困惑。我正在尝试制作数学练习课程。但是,当我选择一个操作时,无论我输入什么,它总是需要我添加。有人可以帮我解决这个问题。我的小弟弟不是很兴奋地等着。 :)

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

3 个答案:

答案 0 :(得分:1)

这是你的choinceOperation函数中的问题。 if statemnts应该是这样的:

if operationChoice in ("add", "+", "adding", "addition"):

您的代码总是会导致“+”的原因是:

operationChoice == "add" or "+" or "adding" or "addition"
# (operationChoice == "add") or "+" or "adding" or "addition"
# and the first fails, but the "+" will always be True.

答案 1 :(得分:1)

使用此功能并参阅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")

答案 2 :(得分:0)

这一行

if operationChoice == "add" or "+" or "adding" or "addition":

没有做你认为它正在做的事情。 它正在检查operationChoice == "add""+" or "adding" or "addition"中是否有任何一个评估为true。 (所有非空字符串计算为True)

要么成功

if operationChoice in ("add", "+", "adding", "addition"):

OR

if operationChoice == "add" or operationChoice == "+" or operationChoice == "adding" or operationChoice == "addition":