[英]How to turn inputs from one variable into a list?
So I am new to python and started to design and simple calculator.所以我是 python 的新手,开始设计和简单的计算器。 What it does right now is just ask for number, then ask for operation, then ask for number... etc. I want the numbers that are inputted from the user to be stored in a list.
它现在所做的只是询问号码,然后询问操作,然后询问号码......等等。我希望将用户输入的号码存储在列表中。 Right now when I
print(input_num)
it just prints the last inputted number.现在当我
print(input_num)
它只打印最后输入的数字。 I want to be able to print all of the numbers inputted by the user.我希望能够打印用户输入的所有数字。 Thanks, I hope this was more specific than the last time I posted this.
谢谢,我希望这比我上次发布的更具体。
try:
done = False
next1 = False
input_num = []
print("*** Enter done to calculate result ***")
print("*** Operation list: +, -, *, / ***")
while not done:
input_num = float(input("Enter a number: "))
next1 = True
while next1:
input_operation = input("Enter an operation, or enter \"done\" to finish: ")
next1 = False
if input_operation == "done":
done = True
print(input_num)
except ValueError:
print("*** Invalid Input! ***")
You're looking for .append()
.您正在寻找
.append()
。 This will add the input to the end of your list, so in your case it would be.这会将输入添加到列表的末尾,因此在您的情况下是这样。
input_num.append(float(input("Enter a number: ")))
rather than而不是
input_num = float(input("Enter a number: "))
In the current state it's just setting input_num to the value of the input over and over.在当前的 state 中,它只是一遍又一遍地将 input_num 设置为输入的值。
You can read more on lists here (python 3.8.5 docs).您可以在此处阅读有关列表的更多信息(python 3.8.5 文档)。
While @Codesidian has already provided you a response, I wanted to also address the boolean variables defined.虽然@Codesidian 已经为您提供了回复,但我还想解决定义的 boolean 变量。 You don't need to define so many boolean variables.
您不需要定义这么多 boolean 变量。 Instead you can use True and False in your while loops.
相反,您可以在 while 循环中使用 True 和 False。 Also, you need only one while loop for the entire processing.
此外,整个处理过程只需要一个 while 循环。
I have simplified your code.我已经简化了你的代码。 Hope this helps.
希望这可以帮助。 The functionality has not changed.
功能没有改变。
try:
input_num = []
print("*** Enter done to calculate result ***")
print("*** Operation list: +, -, *, / ***")
while True:
input_num.append(float(input("Enter a number: ")))
input_operation = input("Enter an operation, or enter \"done\" to finish: ")
if input_operation.lower() == 'done':
break
else:
input_num.append(input_operation)
print(input_num)
except ValueError:
print("*** Invalid Input! ***")
Output: Output:
For a good run:为了跑得好:
*** Enter done to calculate result ***
*** Operation list: +, -, *, / ***
Enter a number: 12.25
Enter an operation, or enter "done" to finish: +
Enter a number: 15.75
Enter an operation, or enter "done" to finish: done
[12.25, '+', 15.75]
When a bad value is entered:输入错误值时:
*** Enter done to calculate result ***
*** Operation list: +, -, *, / ***
Enter a number: 45
Enter an operation, or enter "done" to finish: +
Enter a number: 35
Enter an operation, or enter "done" to finish: -
Enter a number: a
*** Invalid Input! ***
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.