2

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
2
  • Not what you're asking about, but the slice is wrong. It shouldn't have the hard-coded 8 in 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. Commented Jul 2, 2015 at 19:16
  • Also note that the terminology in this homework assignment is somewhat imprecise. slice does not remove anything from a list, it creates a new list specified by the slice, leaving the original list unchanged. Commented Jul 2, 2015 at 19:20

4 Answers 4

1

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))
Sign up to request clarification or add additional context in comments.

Comments

0

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 

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.