简体   繁体   中英

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). 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 . 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

You can provide a callable and a sentinel value to iter . This will call the callable until it produces the sentinel value. 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 :

>>> 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. 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. You don't need them in Python.

-If you want to dig deeper into python I'd recommend reading the 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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