[Tutor] Why does loop duplicate?

Ken Hammer kfh777 at earthlink.net
Wed Dec 16 14:37:18 EST 2015


Intent is to print "Jack, Kack, ...." with "O" and "Q" delivering a longer suffix.  Instead, I get the printout shown with duplicates and a second deviation with "O" and "Q" as shown.
Why?  IDLE and Shell are Python 2.5.4 running on Windows 8.1.

prefixes = 'JKLMNOPQ'   ###FAILS WITH REPEATS
suffix = 'ack'
suffixb= 'uack'

for letter in prefixes:
    if letter == "O":
        print letter + suffixb
    if letter == "Q":
        print letter + suffixb
    else:
        print letter + suffix

>>> 
Jack
Jack
Kack
Kack
Lack
Lack
Mack
Mack
Nack
Nack
Ouack
Oack
Oack
Pack
Pack
Quack
Qack
>>> 

Thanks,  Ken Hammer



More information about the Tutor mailing list