Please help to explain this printing result:


#1


Exercise 11, section 14 (Loops):

My code is right, I just don't understand why printing result came that way..
Sometimes it prints few letters of 'word' straight after the other and other time it prints it only once.
Secondly i'm wondering how come the '!' are printed..

I expected only the letters to get printed by a permanent structure without the '!' symbol.
Here is my code:

thing = "spam!"

for c in thing:
    print c

word = "eggs!"

for e in word:
    print e
    for g in word:
        print g
        for s in word:
            print s

please run the code and advise.
Thanks!!


#2

The ! is in a string. Strings are basically an array of ASCII characters, so anytime you iterate through a string, every piece of the string will be printed if that's what the for each loop does... Even if you had a space in string, that space is an ASCII character and will be printed.
I hope this clears things up a bit? It's hard to understand exactly what you're having difficulty working through.

EDIT: I forgot about your other question:
I didn't take a lot of time to look through your code and break it down, but the reason it does this is because of the nested for each loops.
What else I want to ask is why the nested loop? What it looks like to me, is that you're trying to say for the letter e in the variable word, "eggs!", you're trying to print each the letters? That's not how the for each loop works. If you're trying to print each letter of a string, you literally just need to say for ___ in word. e, g, and s are all placeholders, and the computer is not reading them as part of the variable word, because they are not strings. You can print the letters of "eggs!" by just saying:

for letter in word
    print letter

You could literally put anything in the place of letter.
Letter is a variable.


#3

This is what comes up in the black screen:

s
p
a
m
!
e
e
e
g
g
s
!
g
e
g
g
s
!
g
e
g
g
s
!
s
e
g
g
s
!
!
e
g
g
s
!
g
e
e
g
g
s
!
g
e
g
g
s
!
g
e
g
g
s
!
s
e
g
g
s
!
!
e
g
g
s
!
g
e
e
g
g
s
!
g
e
g
g
s
!
g
e
g
g
s
!
s
e
g
g
s
!
!
e
g
g
s
!
s
e
e
g
g
s
!
g
e
g
g
s
!
g
e
g
g
s
!
s
e
g
g
s
!
!
e
g
g
s
!
!
e
e
g
g
s
!
g
e
g
g
s
!
g
e
g
g
s
!
s
e
g
g
s
!
!
e
g
g
s
!
None

How this order of printing makes sense?


#4

I know, I ran it as well. The reason why is because of that nested loop though.
Tell me this:
What exactly do you want to print?


#5

I expected to get that result:

s
p
a
m
!
e
g
g
s
None


#6

For example how comes it prints after "spam!" 2 times the letter 'e' and then the word 'eggs!'
?


#7

Okay, well if you want to get that result, without the ! on the end of eggs like you just posted, lets take a look at your for each loop.

This is an extreme, unneeded nested loop that will print all kinds of results. Let me say that word is actually just "is" for the purpose of this explanation. First with this loop,

for e in word:
    print e

this is print "i" and "s" to the console because e is a variable that changes, but really, it is each letter of the variable word. Next, lets look at the entire loop that I pasted above, (the big chunk), if word is "is", let's look at how the loop will work.
Each time a letter is printed, in the first loop, the second loop says that the entire word will be printed again. Then a third time in your third loop, you're printing your entire loop.
So it goes like this:
i from "is" is printed, then the second for loop starts and "i" is printed again. Next, the THIRD for loop starts, and i is printed one last time. Since the most indented loop has to finish before the rest can, it will finish printing "is", and print "s". Then, it's the second for loops turn to print "s" because it needs to finish before the first one can finish. Lastly, the first for loop will print "s".
If this is hard to follow, imagine how it is to print a word like "eggs!"

To get your desired

to print, you just need to write

for letter in word: #each character in word is a letter
    if != "!": #this is making sure that the explanation point isn't printed
        print letter #this last line just prints all letters in word, 
        #except for the explanation point

#8

What I told you that you need to write, that is not all of the code, that's just your second loop. It just needs to go in place of that nested loop I was referring to, and everything else can stay the same.


#9

Alright I think I've got it :slight_smile:
Last clarification please: the letters used as variables inside the for loops are connected somehow the word's letters stored in the variable 'word' as a literal values? or it does't mean anything if the variable letter appears inside the 'word' variable as well?


#10

In your for each loop, you can use

for letter in word:
    print letter

Letter is a variable that changes during each loop. So the first time it loops, letter will be "e", the second time, "g", the third time, "g", etc.


#11

Great, that's what I was searching for..

Thanks a lot!!!


#12

No problem! Stoked that you understand it now. :slight_smile:


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.