简体   繁体   English

如何将一个变量的输入变成一个列表?

[英]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.

相关问题 如何将多个列表从一个变量转换为一个列表 - How to turn multiple lists from one variable into one list 如何将多个列表从一个变量转换为一个列表 (odoov11)? - How to turn multiple lists from one variable into one list (odoov11)? 如何将循环的结果变成一个列表,然后变成一个变量? - How to turn the result of a loop into a list, and then a variable? 如何将返回的字典变成一个大列表? - How to turn returned Dictionaries into one large list? 如何将列表元素一分为二? - How to turn one list element in two? 如何将这个基本列表变成一个结构化的列表? - How turn this basic list into a more structured one? 如何将列表(大小为一)转换为字典? - How to turn a list (of size one) into a dictionary? 配置解析器从ini文件中抓取单词后,如何将“单词列表”转换为变量 - how to turn a “list of words” into a variable after config parser grabs them from an ini file 如何在 Python 中将数据从文本转换为变量? - How to turn data from Text to Variable in Python? 如何使无效输入仅出现在数字输入中? 以及如何将输入转换为列表并增加每个输入的计数? - How do I make the invalid input appear for only number inputs? And how do I turn the inputs into a list and increase the count for every input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM