循环时保留列表项

时间:2017-03-04 13:30:41

标签: python list

所以我想在这里添加列表来保存循环::

的每个循环中的所有和结果
def calc ():

 addition = []
 sub = []
 multi = []
 division = []
 firstNumber =float(input("Enter the first number: \n"))
 oper =input("choose the operation (+ , - , * , /) \n")
 secondNumber =float(input("Enter the second number: \n"))

 if oper == "+":
     x = firstNumber + secondNumber
     addition.append(x)
     print(x)
     print(addition)
 elif oper == "-":
        firstNumber - secondNumber
 elif oper == "*":
        firstNumber * secondNumber
 elif oper == "/":
    if secondNumber != 0:
        firstNumber / secondNumber
    else:
        print("Can't divide by zero")

while 0<1:
 calc()
 msg = input("Do you want to continue? y/n")
 if msg == "y":
    calc()
 elif msg =="n":
    break
 else:
    print("wrong choise")

所以如果我的第一个加法结果是5而第二个是10 ... 添加列表为[5,10,...]

1 个答案:

答案 0 :(得分:1)

将所有代码保存在while循环中,除了代码的前4行。