[Tutor] Select a string

Ben Finney ben+python at benfinney.id.au
Wed Sep 6 01:54:31 EDT 2017


Pat Martin <wpmartin at gmail.com> writes:

> My eyes are crossing looking at the code, can someone give me a hint
> on what I am missing that causes this to not give me the correct
> answer of 2.

It's good to admit that looking at the code has ceased to help :-)

So, try a different approach. Without using the code, can you describe
your algorithm? Make a pseudo-code description of how you expect to
transform the input to the output.

For example, I might describe the algorithm for reversing a text
string::

    * Given a text string named ‘input_text’
    * Set the output, named ‘output_text’, to the value "".
    * Set the index (named ‘index’) to the character length of ‘input_text’.
    * While ‘index’ is greater than 0:
      * Subtract 1 from ‘index’.
      * Get the character at index ‘index’ of ‘input_text’, assign the
        name ‘input_char’ to that character.
      * Append ‘input_char’ to ‘output_text’.
    * Return the value of ‘output_text’.

Can you describe your algorithm that way, so we can see what is your
intention for the code?

-- 
 \       “Faith is the determination to remain ignorant in the face of |
  `\                 all evidence that you are ignorant.” —Shaun Mason |
_o__)                                                                  |
Ben Finney



More information about the Tutor mailing list