Python for Everybody - Iterations: Loop Idioms

Tell us what’s happening:

for this code in Python, the answer should be 3 but instead, it’s 6. why?

smallest = None
print("Before:", smallest)
for itervar in [3, 41, 12, 9, 74, 15]:
    if smallest is None or itervar < smallest:
        smallest = itervar
        break
    print("Loop:", itervar, smallest)
print("Smallest:", smallest)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Python for Everybody - Iterations: Loop Idioms

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Just to be clear are you answering this question?

One line has an error that will cause the code to not work as expected. Which line is it?:

I’m sorry for the misunderstanding. I didn’t catch the details in the question the first time, my bad. Thanks so much for your answer everything is clear now!

1 Like