简体   繁体   中英

Using a while loop to return a function

In main function:

Create a list that holds the surnames of recent USA presidents, starting with Kennedy and ending with Obama, in chronological order. use a for loop to iterate over the entire list, printing each president's name on its own line. make a slice by removing the first two presidents and the last two presidents from the list. pass the new slice as an argument to a custom function named playlist. use a while loop to display the elements in the list returned by playlist.

In playlist function:

print the size of the sliced list. Use a list function. sort the sliced list in reverse alphabetical order. return this list to main.

This is what i have so far. I can't figure out how to insert the while loop. Every time I do it the list just keeps going or it doesn't show up.

def main():
    #Create list.
    names = ['Kennedy', 'Johnson', 'Nixon', 'Ford', 'Carter', 'Reagan', 'Bush', 'Clinton', 'Bush', 'Obama']
    sliced_list = names[2:8]

    #Display the list.
    print('Here are the most recent presidents of the USA')
    for n in names:
    print(n)
    sliced = playlist(sliced_list)


def playlist(sliced_list):
    size = len(sliced_list)
    print('The list size is now', size)
    sliced_list.sort()
    sliced_list.reverse()
    return sliced_list

main()

This is how it should come back.

Original list in main:
Kennedy
Johnson
Nixon
Ford
Carter
Reagan
Bush
Clinton
Bush
Obama
Not in main: list size is now 6
Back in main, list in reverse alpha order
Reagan
Nixon
Ford
Clinton
Carter
Bush

To iterate over the list elements using a while loop without modifying the list:

i = 0
while i < len(sliced):
    print(sliced[i])
    i += 1

If you prefer the approach suggested by browskie that mutates the list, I would suggest avoiding the try-except block as follows:

while len(sliced):
    print(sliced.pop(0))

Python while loop is a simple loop that loops as long as its condition is met, so if you do something like while i<5: it will loop till i becomes equal to or greater than 5.

Also, in python, you can use len(lst) to get the length of a list also you can do lst[i] (where i is an integer called the index) to get the element at that position, Example in your original list, if we do names[1] it will return Johnson .

Now in your while loop you want to start looping from 0 to till the length of the list and print the list element at each index. I am hoping you can create the program you need based on all the information.

You can try something like this:

sliced = playlist(sliced_list) #This is from your code

while True: #Will run forever
    try:
        #And try to remove and print out the first item from the list
        print(sliced.pop(0)) 
    except:
        #As soon as it exhausts the list and fails, it will stop the loop
        break 

I think you were almost there. The while loop will continue until the sliced list is empty.

def main():
    #Create list.
    names = ['Kennedy', 'Johnson', 'Nixon', 'Ford', 'Carter', 'Reagan', 'Bush', 'Clinton', 'Bush', 'Obama']
    for name in names: #for to print names
        print name
    sliced = playlist(names[2:-2]) # slice 
    while len(sliced): # while there is names in sliced do:
        print sliced.pop() # removes the last name on sliced and prints it

def playlist(sliced_list):
    size = len(sliced_list)
    print('The list size is now', size)
    sliced_list.sort()
    sliced_list.reverse()
    return sliced_list

main()

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