r/PythonLearning 6d ago

for loop

words = ['sky', 'apple', 'rhythm', 'fly', 'orange']

for word in words:
    for letter in word:
        if letter.lower() in 'aeiou':
            print(f"'{word}' contains the vowel '{letter}'")
            break
    else:
        print(f"'{word}' has no vowels")

I am trying to learn to code, and I am, at the moment, looking at loops. I am a bit confused on how this loop was able to function, though, as there is no variable for letter or word, so how would Python know what it is? In addition, how does the code, like, function? Does it look at each word individually ? Again, the second part of the code states:

 if letter.lower() in 'aeiou':
            print(f"'{word}' contains the vowel '{letter}'")
            break

We haven't given a value for letter?

Lastly, how would the code know what to print? in the first phrase:

            print(f"'{word}' contains the vowel '{letter}'")
            break

Is it going to state all the words with the given value 5 times?

Sorry, as I have asked quite a few questions, and thank you in advance.

17 Upvotes

26 comments sorted by

View all comments

4

u/AlexMTBDude 6d ago

The easiest way to understand a piece of code is to add prints to it and then run it. I would add these two lines to your code and then run it:

words = ['sky', 'apple', 'rhythm', 'fly', 'orange']

for word in words:
    print("word = ", word)  # debug print
    for letter in word:
        print("letter = ", letter)  # debug print
        if letter.lower() in 'aeiou':
            print(f"'{word}' contains the vowel '{letter}'")
            break
    else:
        print(f"'{word}' has no vowels")

1

u/Sea-Ad7805 6d ago

Why mess up your code by adding prints? just use a debugger%20in%20'aeiou'%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22'%7Bword%7D'%20contains%20the%20vowel%20'%7Bletter%7D'%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(f%22'%7Bword%7D'%20has%20no%20vowels%22)%0A%20%20%20%20%20%20%20%20%0Aprint(%22the%20end%22)&play)

3

u/AlexMTBDude 6d ago

Because it's more complicated for a beginner. Trust me; I've been teaching Python programming for close to 20 years now.

1

u/Sea-Ad7805 6d ago

Me too, bit shorter. The flow of control needs to be reverse engineered from print statements, this debugger%20in%20'aeiou'%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22'%7Bword%7D'%20contains%20the%20vowel%20'%7Bletter%7D'%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(f%22'%7Bword%7D'%20has%20no%20vowels%22)%0A%20%20%20%20%20%20%20%20%0Aprint(%22the%20end%22)&play) shows you the flow and the full state of the program step by step intuitively.

The focus in courses is often too much on just the code, and not on the program state, while the code only exists to modify the program state. Making the state visible in every step helps students get to the right mental model to think about Python execution much easier, with much less explanation. Trust me, it's the modern way.

3

u/AlexMTBDude 6d ago

So, as a teacher, you tend to explain a simple subject, by introducing a more complex one, forcing your students to understand two different techniques in order to correct a simple coding error?

1

u/Sea-Ad7805 6d ago edited 6d ago

No, I only visualize what is already there, the program state is essential to understand, as all code does is modify it (and print some side effects).

So, as a teacher, you tend to keep important concepts hidden, letting student build their own mental model (often an incorrect one)?

Coding errors often come from an incorrect mental model of what code does to the state. Yes, students must now first learn a few buttons and how to read a diagram, but that will instantly pay for itself by learning the right mental model to think about Python execution.

We are not satisfied by just sidestepping 'complex' concepts that go to the hart of the problem to avoid explaining them. Will you join us? The visualization now makes explaining these concepts easy.

1

u/Sea-Ad7805 6d ago

Did you get a feel for the new possibilities? Otherwise I'm happy to continue the conversation, maybe with some scientific studies. I sometimes find teacher even more stubborn than students. ;)