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
sliceis wrong. It shouldn't have the hard-coded8in it. What if the list started with Johnson instead of Kennedy? You would remove the wrong number of elements at the end. Read up on slices and see if there is a way to always remove the last two elements even if the list length is different.slicedoes not remove anything from a list, it creates a new list specified by the slice, leaving the original list unchanged.