Home CPSC 110

List Reversal

 

Objective

To get experience with using lists in programs.


 

Task

For this lab you will write a program which reads in a list of strings from the user. You can use the empty string "" as a terminal value. So the user can enter as many strings as they want, then hit enter without entering anything to indicate they are done. Make sure you don't put the "" into the list!

Your program will then print out the list of strings backwards. There are two main ways to do this:

  1. Put the strings into the list backwards right when you read them in. This will build the list backwards, and then you can print it out forwards.
  2. Put the strings into the list in order, then loop through them backwards when you print them. You can use a for loop with range to go through the indexes in reverse order.

 

Example Run

Here is an example run:

Enter a string: These
Enter a string: are
Enter a string: some example
Enter a string: words
Enter a string: 
words
some example
are
These

Notice that it reverses the lines. If you put multiple words in one string, like "some example", they aren't reversed themselves. That's OK here!


 

Submitting

When you are finished, please submit the .py file for the lab on Canvas. To do so, you'll need to navigate to where you saved the file on your computer.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.