Note that there are some explanatory texts on larger screens.

plurals
  1. POThink Python (How to think like a computer scientist) - Excercise 8.4
    text
    copied!<p>I'm having some trouble solving the problem below (My thinking on how I might solve it is directly beneath the problem)</p> <blockquote> <p>"In Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack."</p> </blockquote> <p>This loop outputs these names in order:</p> <pre><code>prefixes = 'JKLMNOPQ' suffix = 'ack' for letter in prefixes: print letter + suffix </code></pre> <p>The output is:</p> <pre><code>Jack Kack Lack Mack Nack Oack Pack Qack </code></pre> <p>Of course, that’s not quite right because “Ouack” and “Quack” are misspelled. </p> <p>Exercise 8.2 Modify the program to fix this error."</p> <p>So prefix + suffix yields 4 characters in its current form. Ouack and Quack are 5 characters. In the prior section, we used 'while' to transverse a string to create a condition where if the index is equal to the length of the string, the condition is false, and the body of the loop is not executed. I'm thinking I need to modify the for loop so that if the output character count is less than the original string value inputted, it adds the letter 'U'. How do I go about relating the length of the string value inputted for each name and the 'prefixes'?</p> <p>I'm not sure how I'd go about doing this, or if I should persue a different strategy.</p> <p>I apologize in advance I've butchered any of the terminology, I'm two days into this book, and I have no prior computer science background.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload