循环到用户提示

时间:2015-10-18 07:28:36

标签: python loops

我是python编程的初学者,我的代码遇到了问题。

当用户键入无效操作时,它会通知用户但退出程序(第33行)。如何让它让用户再次输入数学运算?

#This python program calculates the sum, difference, product, or quotient of two numbers defined by users.

#Define add function and return the result of num1 + num2
def add(num1, num2):
    return num1 + num2

#Define subract function and return the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

#Define multiplication function and return the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

#Define division function and return the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2


#Define main purpose/function of the program
def main():

    #Ask what math operation to perform
    operation = input("What do you want to do? (+, -, *, /): ")

    #If the operation is not +, -, *, or /, invalid operation
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        print("You must enter a valid operation!")

    #If valid, perform specified operation
    else:
        var1 = int(input("Enter num1: "))
        var2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(var1, var2))
        elif(operation == '/'):
            print(div(var1, var2))
        elif(operation == '-'):
            print(sub(var1, var2))
        else:
            print(mul(var1, var2))

main()

2 个答案:

答案 0 :(得分:1)

只需要求用户再次输入:

#This python program calculates the sum, difference, product, or quotient of two numbers defined by users.

#Define add function and return the result of num1 + num2
def add(num1, num2):
    return num1 + num2

#Define subract function and return the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

#Define multiplication function and return the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

#Define division function and return the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2


#Define main purpose/function of the program
def main():

    #Ask what math operation to perform
    operation = input("What do you want to do? (+, -, *, /): ")

    #If the operation is not +, -, *, or /, invalid operation
    while (operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        print("You must enter a valid operation!")
        operation = input("What do you want to do? (+, -, *, /): ")

    var1 = int(input("Enter num1: "))
    var2 = int(input("Enter num2: "))
    if(operation == '+'):
        print(add(var1, var2))
    elif(operation == '/'):
        print(div(var1, var2))
    elif(operation == '-'):
        print(sub(var1, var2))
    else:
        print(mul(var1, var2))
 main()

答案 1 :(得分:0)

如果您使用的是Python2,则无法在此处使用输入,因为input()会评估执行上下文中的输入。所以,你应该使用raw_input()。对于Python-3.x,您可以使用input()。

就您的问题而言,您可以将其置于while循环中。

#This python program calculates the sum, difference, product, or quotient of two numbers defined by users.

#Define add function and return the result of num1 + num2
def add(num1, num2):
    return num1 + num2

#Define subract function and return the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

#Define multiplication function and return the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

#Define division function and return the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2


#Define main purpose/function of the program
def main():

    while True:
      #Ask what math operation to perform
      operation = raw_input("What do you want to do? (+, -, *, /): ")
      print "operation is ", operation, type(operation)

      #If the operation is not +, -, *, or /, invalid operation
      if operation != '+' and operation != '-' and operation != '*' and operation != '/':
        print("You must enter a valid operation!")
      #If valid, perform specified operation
      else:
        var1 = int(input("Enter num1: "))
        var2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(var1, var2))
        elif(operation == '/'):
            print(div(var1, var2))
        elif(operation == '-'):
            print(sub(var1, var2))
        else:
            print(mul(var1, var2))
        return 0

main()