简体   繁体   English

如何将变量添加到 PyCharm 的列表中?

[英]How do you add Variables to lists in PyCharm?

I am pretty new to Python, I'm using PyCharm using Python 3.9 I was wondering if anyone knew how to add user input variables to lists.我对 Python 很陌生,我正在使用 PyCharm 使用 Python 3.9 我想知道是否有人知道如何将用户输入变量添加到列表中。 I have tried.insert.append and It keeps giving me the same, "Name 'variable' can be undefined" error.我已经尝试过.insert.append 并且它一直给我同样的信息,“名称'变量'可以是未定义的”错误。 If anyone has any knowledge on how to fix it, it would be greatly appreciated.如果有人对如何修复它有任何了解,将不胜感激。

The correct method is .append , it adds an element to the end of a list.正确的方法是.append ,它将一个元素添加到列表的末尾。 You can also use .insert , which inserts that element at a certain index.您还可以使用.insert ,它将该元素插入到某个索引处。

The warning "Name 'variable' can be undefined" is PyCharm's way of letting you know that the definition of the list you are trying to append to might not be executed.警告"Name 'variable' can be undefined"是 PyCharm 让您知道您尝试 append 的列表定义可能无法执行的方式。

For example, say you have the following code:例如,假设您有以下代码:

for a in somelist:
    someotherlist = ['a', 'b', 'c']
someotherlist.append('d')

You'd think this would always work, but what if somelist is empty?你会认为这总是有效的,但是如果somelist是空的呢? Then the for loop will never happen.那么for循环将永远不会发生。 So Python will try to append to a list that does not exist ( someotherlist ), which it cannot do.所以 Python 将尝试 append 到一个不存在的列表( someotherlist ),这是它做不到的。

So, make sure that the definition of the list you are trying to append to will always run before you try to append to it.因此,在您尝试 append 之前,请确保您尝试 append 的列表定义将始终运行。

In Python, you can create a empty list like this:在 Python 中,您可以像这样创建一个空列表:

example_list = []

or或者

example_list = list()

To append elements to a list, you can use the append method of the list.要将 append 元素添加到列表中,可以使用列表的 append 方法。

example_list.append( 'This is a list item' )

You can read more about the Python lists on the official documentation .您可以在官方文档中阅读有关 Python 列表的更多信息。

The error that you are getting basically says that, it can not find the variable by the name of 'variable'.您得到的错误基本上是说,它无法通过“变量”的名称找到变量。

When asking a question on stack overflow, please provide the code in question, so we can assist you better.在询问堆栈溢出问题时,请提供相关代码,以便我们更好地为您提供帮助。 So go ahead and post the code, so we can help you better.因此,请提前 go 并发布代码,以便我们更好地帮助您。

If I am understanding your question correctly...如果我正确理解你的问题......

The input() command should give you the functionality you are wanting. input()命令应该为您提供所需的功能。 The code simply runs through a loop and accepts user input at the beginning of each iteration.代码只是通过一个循环运行,并在每次迭代开始时接受用户输入。 That input is then 'appended' to the current list using the append() method of the list object.然后使用列表 object 的append()方法将该输入“附加”到当前列表。 If you are wanting to stop the loop then you would pass STOP如果您想停止循环,那么您将通过STOP

my_list = list()
STOP_STRING = 'STOP'
while True:
    value = input("Please Enter a value \n")
    if value == 'STOP':
        break
    my_list.append(value)

print(my_list)

Example output:示例 output:

>>>Please Enter a value
>>>Hello
>>>Please Enter a value
>>>World
>>>Please Enter a value
>>>STOP
>>>['Hello', 'World']

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

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