简体   繁体   English

使用while循环返回一个函数

[英]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.使用 for 循环遍历整个列表,在自己的行上打印每个总统的名字。 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.将新切片作为参数传递给名为 playlist 的自定义函数。 use a while loop to display the elements in the list returned by playlist.使用 while 循环显示播放列表返回的列表中的元素。

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.我不知道如何插入 while 循环。 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:要使用while循环遍历列表元素而不修改列表:

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:如果您更喜欢 browskie 建议的改变列表的方法,我建议避免使用 try-except 块,如下所示:

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. Python while 循环是一个简单的循环,只要满足其条件就会循环,因此如果您执行类似while i<5: ,它将循环直到i等于或大于 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 .此外,在python中,您可以使用len(lst)来获取列表的长度,也可以执行lst[i] (其中 i 是一个称为索引的整数)来获取该位置的元素,例如在原始列表中,如果我们执行names[1] ,它将返回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.现在在您的 while 循环中,您希望从 0 开始循环到列表的长度,并在每个索引处打印列表元素。 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. while 循环将继续直到切片列表为空。

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

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

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