【问题标题】:Python3: If elif else with while loops in a functionPython3:如果在函数中使用while循环的elif else
【发布时间】:2017-10-13 01:33:03
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    问题出在您的 choinceOperation 函数中。 if 语句应如下所示:

    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.
    

    【讨论】:

      【解决方案2】:

      这一行

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

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

      要么成功

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

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

      【讨论】:

      • 谢谢!很好的解释。
      【解决方案3】:

      使用这个并参考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")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-02
        • 2021-04-15
        • 2021-10-31
        • 2017-01-19
        • 1970-01-01
        • 2012-06-03
        • 2019-11-26
        • 2017-06-07
        相关资源
        最近更新 更多