Reverse (the time you've spent reading this)


#1

I have some ideas how to write the code from a different perspective. Probably, another inefficient one.
I just want to understand my mistakes.






#2

When I run “reverse(“abcd”)” with your code, I get the following error:

Traceback (most recent call last):
File “python”, line 10, in
File “python”, line 5, in reverse
TypeError: ‘bool’ object is not iterable

I don’t know what that means, because it says that a boolean object is not iterable, but the instructions said that the function should take a string, not a boolean, so I don’t understand what the error is…


#3

I’ll ask someone else to help us here:

@stetim94 please help us with this issue!


#4

I think he maybe sleeps, in Denmark the time is 00.18 AM :slight_smile:


#5

Thanks a lot for your concern!


#6

You’re welcome, I’m just happy to try help you :slight_smile:


#7

And I think he will reply soon :grinning:


#8

The dude never catches a break, @vicedivic :rofl:, but hey @eternal_aelita, I’ll try to help you out by breaking it down a little. :slight_smile:

Starting at line 1 of your code:

  • we define def our function reverse() that takes one parameter text. So reverse(text).

Line 2:

  • text = [] as now been replaced with an empty list, so text is basically empty. How do we turn our parameter (text) into a list without effecting it’s value? :wink:

At line 3:

  • new_word = [] is an empty list.

Line 5:

  • for n in range(0, len(text)) in text:
    How is this going to work out? text is empty, so reconsider how to iterate through your list. (Pretend I removed the last text in your for loop…)

Line 6:

  • new_word.insert((len(text) - n), text[n])
    So here, we have our empty list new_word and insert the key of text[n] based on the position from the length of text - n.
    Okay, so big problem: how are we going to do this if we’re trying to reverse a string? Read on insert().
    TypeError: list indices must be integers, not str (text[n])
    And we can’t subtract a letter from a letter; we’d get an error!

Finally, on line 7:

  • our function ends when the return keyword is reached, so return new_word is where it ends. But, our result is none! That occurred when we tried inserting into new_word.
    Even if I unindented it to try to get the full iteration or switched what to return, it would do nothing. And the last print statement won’t execute because it’s indented after the return keyword.

I hope this helps.

If any of you see that I’m wrong on something, let me know and I’ll fix it. :wave:


#9

Does he not sleep at all?


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.