简体   繁体   English

使用循环创建用户输入名称列表

[英]creating a list of user input names using loop

I'm trying to write a loop that allows a user to input names, until the user clicks Enter without entering a name (enters an empty string).我正在尝试编写一个循环,允许用户输入名称,直到用户单击 Enter 而不输入名称(输入一个空字符串)。 Once the list input is complete, print the list, sort the list, and then print the sorted list.列表输入完成后,打印列表,对列表进行排序,然后打印排序后的列表。 This is what I have so far.这是我到目前为止。

ListOfNames=[]
while True:
    Name=raw_input('-->')
    if Name=="":
    break
else:
    ListOfNames.append(Name)
    print ListOfNames
    ListOfNames=sorted(ListOfNames)
    print ListOfNames

What you want to do is called a while loop with a sentinel .你想要做的是一个带有sentinel的 while 循环。 When using a sentinel, the first input will be outside the loop.使用哨兵时,第一个输入将在循环之外。 You can implement such method like that:你可以实现这样的方法:

names = []
input = raw_input("-->")
while not input == "":
    names.append(input)
    input = raw_input("-->")

Then you can sort it and do whatever you want with the list.然后你可以对它进行排序,并对列表做任何你想做的事情。
Read more about sentinels here: https://en.wikipedia.org/wiki/Sentinel_value在此处阅读有关哨兵的更多信息: https : //en.wikipedia.org/wiki/Sentinel_value

You can provide a callable and a sentinel value to iter .您可以为iter提供一个 callable 和一个 sentinel 值。 This will call the callable until it produces the sentinel value.这将调用 callable 直到它产生哨兵值。 Demo:演示:

>>> ListOfNames = list(iter(raw_input, ''))
nameB
nameA
nameC

>>> print(ListOfNames)
['nameB', 'nameA', 'nameC']
>>> ListOfNames.sort()
>>> print(ListOfNames)
['nameA', 'nameB', 'nameC']

If you need the prompt, combine with functools.partial :如果您需要提示,请结合functools.partial

>>> from functools import partial
>>> ListOfNames = list(iter(partial(raw_input, '--> '), ''))
--> nameB
--> nameA
--> nameC
--> 
>>> print(ListOfNames)
['nameB', 'nameA', 'nameC']
>>> ListOfNames.sort()
>>> print(ListOfNames)
['nameA', 'nameB', 'nameC']

When indented properly, your code behaves the way I believe you're going for:当缩进正确时,您的代码将按照我认为您想要的方式运行:

ListOfNames=[]
while True:
    Name=raw_input('-->')
    if Name=="":
        break
    else:
        ListOfNames.append(Name)
print ListOfNames
ListOfNames=sorted(ListOfNames)
print ListOfNames
ListOfNames=[]
while True:
    Name=raw_input('-->')
    if Name=="":
        print ListOfNames
        print sorted(ListOfNames)
        break
    else:
        ListOfNames.append(Name)

This should do it, although there are a lot of ways to do it:这应该可以做到,尽管有很多方法可以做到:

ListOfNames=[]
while True:
    Name=raw_input('-->')
    if Name=="":
        break
    else:
        ListOfNames.append(Name)

print ListOfNames
print ListOfNames.sort()

The best way to do this is use your termination clause as your while condition.最好的方法是使用终止子句作为 while 条件。 This code asks for a name, and as long as that name isn't empty (equal to ""), asks for another.此代码要求输入名称,只要该名称不为空(等于“”),就会要求输入另一个名称。 When the user doesn't give a name, the loop terminates.当用户没有给出名字时,循环终止。

ListOfNames=[]

Name = raw_input('-->')
while Name != "":

    ListOfNames.append(Name)
    print ListOfNames
    Name = raw_input('-->')

ListOfNames=sorted(ListOfNames)
print ListOfNames

This should do the trick:这应该可以解决问题:

ListOfNames=[]

while True: #{
    Name=raw_input('-->')
    if Name=="": #{
        break
    #}
    else: #{
        ListOfNames.append(Name)
    #}
#}

print ListOfNames
ListOfNames=sorted(ListOfNames)
print ListOfNames

Her a few tips: -Your logic was correct, your indentation wasn't.她的一些提示: - 你的逻辑是正确的,你的缩进不是。 Think of it as the curly brackets like in C or Java.将其视为 C 或 Java 中的大括号。 You don't need them in Python.在 Python 中不需要它们。

-If you want to dig deeper into python I'd recommend reading the PEP8 . - 如果您想深入了解 Python,我建议您阅读PEP8 You don't need to follow it by heart, but in general it helps producing clean code, which is a lot easier to read and to debug.您不需要牢记它,但通常它有助于生成干净的代码,这更易于阅读和调试。

More easy to read :更容易阅读:

ListOfNames=[]
while True:
    Name=raw_input('-->')
    if Name != "": ListOfNames.append(Name)
    else : break
print (ListOfNames,sorted(ListOfNames))

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

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