如何回到代码的开头?

时间:2019-03-29 14:42:20

标签: python-3.x

我一直在尝试制作此计算器,但我希望它在结束后返回到代码的开头,以便用户可以根据需要进行任意数量的计算,但我不知道该怎么做

我一直在尝试while循环,但是它们似乎不起作用。

这是我到目前为止所拥有的。

def main():

   def add(x, y):
      return x + y

   def subtract(x, y):
      return x - y

   def multiply(x, y):
      return x * y

   def divide(x, y):
      return x / y

   def mod


print ('Hello! I am your personal calculator. Please enter the operation you would like to use.')

print ('Your choices are, addition, subtraction, multiplication, and division!')

x = input('What is your choice?')

num1 = int(input("Please enter your first number"))

num2 = int(input("Please enter your second number"))

if x== 'addition':
   print(num1, "+", num2, "=", add(num1,num2))

elif x== 'subtraction':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif x== 'multiplication':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif  x== 'division':
   print(num1,"/",num2,"=", divide(num1,num2))

else:
   print("Invalid Input")

2 个答案:

答案 0 :(得分:1)

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

def continue_cal():
    resp = input("Continue ? Y/N ")
    if resp == "n" or resp == "N":
        return False
    return True

print('Hello! I am your personal calculator. Please enter the operation you would like to use.')

while True:

    print('Your choices are, addition, subtraction, multiplication, and division!')

    x = input('What is your choice?')

    num1 = int(input("Please enter your first number"))

    num2 = int(input("Please enter your second number"))

    if x == 'addition':
        print(num1, "+", num2, "=", add(num1, num2))

    elif x == 'subtraction':
        print(num1, "-", num2, "=", subtract(num1, num2))

    elif x == 'multiplication':
        print(num1, "*", num2, "=", multiply(num1, num2))

    elif x == 'division':
        print(num1, "/", num2, "=", divide(num1, num2))

    else:
        print("Invalid Input")

    if not continue_cal():
       break

输出:

Hello! I am your personal calculator. Please enter the operation you would like to use.

Your choices are, addition, subtraction, multiplication, and division!
What is your choice?addition

Please enter your first number5

Please enter your second number15

5 + 15 = 20

Continue ? Y/N 

答案 1 :(得分:0)

一种方法可能是以下方式,但是现在用户必须通过输入exit作为输入选择来指定何时退出。

while (True):
    print ('Hello! I am your personal calculator. Please enter the operation you would like to use.')

    print ('Your choices are, addition, subtraction, multiplication, and division!')

    x = input('What is your choice?')

    num1 = int(input("Please enter your first number"))

    num2 = int(input("Please enter your second number"))

    if x== 'addition':
       print(num1, "+", num2, "=", add(num1,num2))

    elif x== 'subtraction':
       print(num1,"-",num2,"=", subtract(num1,num2))

    elif x== 'multiplication':
       print(num1,"*",num2,"=", multiply(num1,num2))

    elif  x== 'division':
       print(num1,"/",num2,"=", divide(num1,num2))
    elif x== 'exit':
        break
    else:
       print("Invalid Input")