简体   繁体   English

如何在 Python 中开始新操作

[英]How to start new operation in Python

So i have a simple calculator in Python .所以我在Python有一个简单的计算器。 So what it does is, asks for an operation (like addition) and it asks for first number and second number .所以它的作用是要求一个操作(比如加法),它要求第一个数字第二个数字

Lets say i choose addition ,假设我选择加法

first number: 1第一个数字: 1

second number: 1第二个数字: 1

result: 2结果: 2

And after that i want it to ask: type {x} to start new calculation然后我想让它问:输入 {x} 开始新的计算

When you type x it basically restarts everything so you can do a different calculation.当您键入 x 时,它基本上会重新启动所有内容,因此您可以进行不同的计算。 ( {x} could be anything i don't mind) ( {x} 可以是我不介意的任何东西)

How do i do that?我怎么做?

current code:当前代码:

print("Which operation do you want to do?")
print("Type + for addition")
print("Type - for subtraction")
print("Type * for multiplication")
print("Type / for division")

op = input('Enter your choice here = ')


if op == '+' :
  num1 = float(input("Enter the first number: "))
  num2 = float(input("Enter the second number here: "))
  add = num1 + num2

  print("{0} + {1} is {2}".format(num1, num2, add))


elif op == '-' :
  num1 = float(input("Enter the first number here: "))
  num2 = float(input("Enter the second number here: "))
  sub = num1 - num2

  print("{0} - {1} is {2}".format(num1, num2, sub))


elif op == '*' :
  num1 = float(input("Enter the first number here: "))
  num2 = float(input("Enter the second number here: "))
  multi = num1 * num2

  print("{0} * {1} is {2}".format(num1, num2, multi))


elif op == '/' :
  num1 = float(input("Enter the first number here: "))
  num2 = float(input("Enter the second number here: "))
  division = num1 / num2

  print("{0} / {1} is {2}".format(num1, num2, division))


else :
  print("something went wrong!")
while True:
    print("Which operation do you want to do?")
    print("Type + for addition")
    print("Type - for subtraction")
    print("Type * for multiplication")
    print("Type / for division")
    print("Type 0 to exit")

    op = input('Enter your choice here = ')

    if op == '+' :
        while True:
            num1 = float(input("Enter the first number: "))
            num2 = float(input("Enter the second number here: "))
            add = num1 + num2
            print("{0} + {1} is {2}".format(num1, num2, add))

            again = input("Would you like to add two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break


    elif op == '-' :
        while True:
            num1 = float(input("Enter the first number here: "))
            num2 = float(input("Enter the second number here: "))
            sub = num1 - num2
            print("{0} - {1} is {2}".format(num1, num2, sub))
            again = input("Would you like to subtract two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break


    elif op == '*' :
        while True:
            num1 = float(input("Enter the first number here: "))
            num2 = float(input("Enter the second number here: "))
            multi = num1 * num2
            print("{0} * {1} is {2}".format(num1, num2, multi))
            again = input("Would you like to multiply two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break


    elif op == '/' :
        while True:
            num1 = float(input("Enter the first number here: "))
            num2 = float(input("Enter the second number here: "))
            division = num1 / num2
            print("{0} / {1} is {2}".format(num1, num2, division))
            again = input("Would you like to divide two other numbers? Y/N: ").capitalize().strip()
            if again == 'Y':
                continue
            else:
                break

    elif op == '0':
        print("Good bye!")
        break
    else :
        print("something went wrong!")

you can place the code inside a function and then call the function within a while loop, the second input again is used to break the loop once the user is finished您可以将代码放在 function 中,然后在 while 循环中调用 function,第二个输入again用于在用户完成后中断循环

print("Which operation do you want to do?")
print("Type + for addition")
print("Type - for subtraction")
print("Type * for multiplication")
print("Type / for division")


def function(op):
    if op == '+' :
        num1 = float(input("Enter the first number: "))
        num2 = float(input("Enter the second number here: "))
        add = num1 + num2
    
        print("{0} + {1} is {2}".format(num1, num2, add))
    
    
    elif op == '-' :
        num1 = float(input("Enter the first number here: "))
        num2 = float(input("Enter the second number here: "))
        sub = num1 - num2
    
        print("{0} - {1} is {2}".format(num1, num2, sub))
    
    
    elif op == '*' :
        num1 = float(input("Enter the first number here: "))
        num2 = float(input("Enter the second number here: "))
        multi = num1 * num2
    
        print("{0} * {1} is {2}".format(num1, num2, multi))
    
    
    elif op == '/' :
        num1 = float(input("Enter the first number here: "))
        num2 = float(input("Enter the second number here: "))
        division = num1 / num2
    
        print("{0} / {1} is {2}".format(num1, num2, division))
    
    
    else:
        print("something went wrong!")

while True:
    op = input('Enter your choice here = ')
    function(op)
    again = input('Enter X to start new calculation: ')
    if again.lower() != 'x':
        break

You can use a while loop to do this.您可以使用 while 循环来执行此操作。 Use boolean variable repeat to decide if the user wants to do another calculation.使用 boolean 变量重复来决定用户是否要进行另一次计算。 If user input is 'x' repeat remains true and the calculator is restarted, but if user inputs anything else repeat is False and the loop exits:如果用户输入为 'x',则重复保持为真并重新启动计算器,但如果用户输入其他任何内容,则重复为假并退出循环:

repeat = True
while repeat:
    #Your code here
    again = input('type x to start new calculation')
    repeat = again in ['x', 'X']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM