如何在Python中启动新操作



所以我在Python中有一个简单的计算器。所以它所做的是,要求一个运算(比如加法(,并要求第一个数第二个数

假设我选择添加

第一个数字:1

第二个数字:1

结果:2

然后我想让它问:键入{x}开始新的计算

当你输入x时,它基本上会重新启动一切,这样你就可以进行不同的计算。({x}可以是任何我不介意的东西(

我该怎么做

当前代码:

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循环中调用该函数,第二个输入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
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!")

您可以使用while循环来执行此操作。使用布尔变量repeat来决定用户是否要进行另一次计算。如果用户输入为"x",则重复保持为真,并重新启动计算器,但如果用户输入任何其他内容,则重复为假,并退出循环:

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

最新更新