Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 附加列表的结束循环_Python_Python 3.x_List - Fatal编程技术网

Python 附加列表的结束循环

Python 附加列表的结束循环,python,python-3.x,list,Python,Python 3.x,List,我需要创建一个程序,该程序在附加列表中请求用户名,然后在用户键入“q”时结束。我已创建了请求用户名的代码,但在结束位置遇到问题。我的循环在我认为应该的时候没有中断。它会继续运行 我尝试过让它成为for循环和while循环,并且在while循环中获得了更大的成功,但我可能不正确 # names of people that I know names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"] while True: names.

我需要创建一个程序,该程序在附加列表中请求用户名,然后在用户键入“q”时结束。我已创建了请求用户名的代码,但在结束位置遇到问题。我的循环在我认为应该的时候没有中断。它会继续运行

我尝试过让它成为for循环和while循环,并且在while循环中获得了更大的成功,但我可能不正确

# names of people that I know
names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    names.append(input("Please enter your first name, "
                       "type q to exit: ", ))
    print(names)
    if names == "q":
        break
我预计结果是:

Please enter your first name, type q to exit: jon
['Billy', 'Trevor', 'Rachel', 'Victoria', 'Steve', 'jon']
Please enter your first name, type q to exit: quit
['Billy', 'Trevor', 'Rachel', 'Victoria', 'Steve', 'jon', 'quit']
  • 我希望程序退出,而不是退出

当检查退出时,您正在比较整个列表,
名称
“q”
。相反,您要检查最近的输入是否为
“q”

可以通过检查列表中的最后一个元素来执行此操作,例如,将条件更改为

if names[-1] == "q"

代码的其余部分可以保持不变。

您应该在另一个名为
name
的变量中输入,并在看到q时中断循环。现在您直接将输入附加到
name
列表中

names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    name = input("Please enter your first name, "
                       "type q to exit: ", )
    if name == "q":
        break
    names.append(name)
    print(names)

你的想法是对的。但是,一旦你从
input()
中得到一个名字,你就可以在检查是否要退出程序之前立即将它插入你的列表中。因此,解决方法是检查退出信号,然后加上名字

# names of people that I know
names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    name = input("Please enter your first name, "
                       "type q to exit: ", )
    if name == "q":
        break
    else:
        names.append(name)
print(names)

问题是,在检查是否为“q”之前,您已经将其添加到列表中。此外,从您的问题中,您只想在“q”上退出。而不是在“quit”或“q”上退出。如果您也想检查“quit”,则应将其添加到
if
条件中

此外,请执行此检查并仅在不是退出条件时追加。因此,我建议:

# names of people that I know
names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    name = input("Please enter your first name, type q to exit: ")
    if name == "q" or name == "quit":
        break
    names.append(name)
print(names)
如果您想坚持您的方法,那么在中断之前,您希望删除最后一个元素,因为这将是“q”或“退出”。因此:

names = ["Billy", "Trevor", "Rachel", "Victoria", "Steve"]
while True:
    names.append(input("Please enter your first name, "
                   "type q to exit: ", ))
    print(names) #Printing here will still show you the name 'q' or 'quit'. Move this down too.
    if names[-1] == 'q' or names[-1] == 'quit':
        names.pop() #This will remove the 'q' or 'quit'
        break

您正在将列表与字符串“q”进行比较,该字符串将始终为false。您可以修改

while True:
    inp = input("Please enter your first name, "
                   "type q to exit: ", )
    names.append(inp)
    print(names)
    if inp == "q":
        break

给出的答案都是正确的,但在您的示例中,您键入的是
quit
而不是
q
@offence 2113是否有我的答案不被接受的原因?它是正确的,并且在突出问题的同时给出了两种方法的选择。它最初被接受。此答案不正确,并且与OP的代码所做的事情相同s、 我正在用q检查inp,而不是数组。它类似于你的solution@ksholla20它仍然会将
q
添加到名称列表中。是的,您正在使用q检查inp,而不是数组,但您已经将输入“q”附加到名称中。这仍然是问题所在。太好了,如果有助于解决断点问题,请向上投票/接受答案但不是“q”被附加到列表中的事实,所以这并不能解决它。