Additional Help

On this page you can find additional hints and help for if you get stuck on the lab. We encourage you to use this information only after giving the problem a few minutes of thought on your own.





Table of Contents
  1. Are you stuck on how you're supposed to use the for loop?
  2. Not sure what you're supposed to be doing?
  3. Getting an "IndexError" error when trying to reverse the string?
  4. Not sure how to store your reversed string?
  5. Still having trouble?



Are you stuck on how you're supposed to use the for loop?

Think about how can you use a for loop to iterate through the string backwards. You may find the Pre Lab section on "range() and for loops" helpful.

(back to top)
































Not sure what you're supposed to be doing?

The general idea of this lab is to take a string, and create a backwards version of the string. You can do this by looking at the original string letter by letter, starting from the back, and saving each of these letters to a new string.

(back to top)
































Getting an "IndexError" error when trying to reverse the string?

The full error is "IndexError: string index out of range". This error messages means that your program is trying to access an index of the string where no character exists. For example, if we have the string "dog", it has three indexes: 0, 1, and 2. If we try to access the character at index 3, which doesn't exist, we'll get this erorr.

If you're getting this error, try printing out the values of your loop variable, instead of using them to index. This way you can see if it's starting and stopping where you want it to. (And remember that Python strings are indexed from 0 to their length-1.)

(back to top)
































Not sure how to store your reversed string?

The first thing you need to do is create and initialize the variable you want to store your reversed string in. Think carefully about where you should initialize it: inside the for loop, or before the loop begins?

Once you've initialized it, remember that we can concatenate two strings together by using the + operator. HINT: this is true even if one string is a single character.

(back to top)
































Still having trouble?

If you're still stuck, you can try using a "debug statement" or two. These are print() statements that give you a bit more information on what exactly is going on. For example, you might want to see what character your for loop is accessing each time:
    print("At index", i, "the character is", word[i])

Or your might want to see how the reversed word is updating:
    print("The reversed word is:", reverseWord)

If you place either of these print() statements inside the for loop where you are creating the new string, it will show you what is going on in the "background" of your program. Each time the for loop is run, the information in your debug statement will be printed to the screen, allowing you to trace what happens with each iteration of the for loop.

Debug statements can be anything that helps you figure out what your program is actually doing. Just don't forget to remove them when you're done!

(back to top)