[Tutor] Re: New to programming question

Jeffrey Maitland maitj at vianet.ca
Tue Apr 12 19:19:08 CEST 2005


Ben Markwell writes: 

> This is an exercise from "How to think like a Computer Scientist." 
> 
> 
> The following example shows how to use concatenation and a for loop to 
> generate an abecedarian series. "Abecedarian" refers to a series or list in 
> which the elements appear in alphabetical order. For example, in Robert 
> McCloskey's book *Make Way for Ducklings*, the names of the ducklings are 
> Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop outputs 
> these names in order:  
> 
> prefixes = "JKLMNOPQ" 
> suffix = "ack"  
> 
> for letter in prefixes: 
> print letter + suffix  
> 
> The output of this program is:  
> 
> Jack 
> Kack 
> Lack 
> Mack 
> Nack 
> Oack 
> Pack 
> Qack  
> 
> Of course, that's not quite right because "Ouack" and "Quack" are 
> misspelled.*
> * 
> 
> *As an exercise, modify the program to fix this error.
> * 
> 
> ==================================================
>  
> In trying to solve the problem I have come up with the following:
>  
> prefixes = 'JKLMNOPQ'
> suffix = 'ack'
> xsuffix = 'uack' 
> 
> 
> for letter in prefixes:
> n = 0
> if prefixes[n] == 'O' or 'Q':
> print prefixes[n] + xsuffix
> else:
> print letter + suffix
>  
> --- I know it doesn't work, but want to know if I am on the right track. And 
> what is the solution?
>  
> Thanks
>  
> Ben
> **

Well you did come up with a way that would work sort of and you seem to be 
ont eh right track. I would make 1 small change if using your approach. 

prefixes = 'JKLMNOPQ'
suffix = 'ack' 

for letter in prefixes:
  if letter == 'O' or letter == 'Q': print letter + 'u' + suffix
  else: print letter + suffix 

However there are other methodes to solve this problem but following the 
logic you are using this works. 

Jeff 




More information about the Tutor mailing list