Long story short I have this exercise to complete:
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 tries to output these names in order.
Of course, that’s not quite right because Ouack and Quack are
misspelled. Can you fix it?
The code that I've been given:
prefixes = "JKLMNOPQ"
suffix = "ack"
for p in prefixes:
print(p + suffix)
After different try I managed to solve it like that:
prefixes = "JKLMNOPQ"
suffix = "ack"
for p in prefixes:
if p=="O" or p=="Q":
print(p + "u" + suffix)
else:
print(p + suffix)
It looks a little bit too much "hardcode" to me. Do you think it is a valid solution ?
Then I tried, just for the sake of comprehension, to do something similar by myself.
So I wrote the following code:
list=["ABCD"]
for l in list:
if l =="A" or l=="B":
print("ok")
else:
print(l)
Basically I want it to print "ok" when L is equal to A or B and L (so "C" and "D") otherwise.
But instead the output is:
ABCD
If I delete the last print command, no output is printed.
What's wrong ? It looks pretty similar to the one I solved but still it doesn't work as I expect.
What am I missing ?
Thank you guys, and happy new year.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…