Python cannot count apparently

Chris Angelico rosuav at gmail.com
Sun Feb 7 14:40:21 EST 2021


On Mon, Feb 8, 2021 at 6:36 AM Philipp Daher via Python-list
<python-list at python.org> wrote:
>
> Hello,
>
> I recently coded this snippet of code:
> myString=„hello“
> for i in range(len(myString):
>      print(string[i])

This code won't work as is. Please *copy and paste* your code when
asking for help.

> And now for the weird part:
>
> SOMETIMES, the output is this:
>
> hello
>
> And SOMETIMES, the output changes to:
>
> ohell

Neither of those makes sense based on the code you're showing, so
again, please *copy and paste* the code and output.

> WHY??? Why do I get different outputs with the EXACT SAME CODE?
>
> Can someone help me please? Thank you

I would recommend that you start by iterating over the string itself,
instead of a range. My crystal ball tells me that you're getting
tangled with negative indexing, but without seeing the code each time,
it's impossible to be sure.

ChrisA


More information about the Python-list mailing list