Guys this answer is 6 , but i do not know how the answer is 6 but please tell me how the answer is no.6 please expain it for me guys

Below is code to find the smallest value from a list of values. One line has an error that will cause the code to not work as expected. Which line is it?:

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)

try executing your code with a tool like this: http://pythontutor.com/

see if you can see what’s the wrong behaviour there

Welcome, moulesh,

I’ve edited your post 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

Can you please reply me with a video on how to display the code in the forum with any example of a code i didn’t understand, please help me with this

there is a gif image in the previous post

anyway, after you have finished writing the post, you can select each code block and press the </> button to have it formatted

I think you can’t compare None value with integer (itarvar < smallest)

Yes but the answer is 6 and the error is in line 6 of the word break

Your code just looks at the first item of the list (which is 3) and puts that into the smallest variable. It works like this: For each item (itervar) in the given list ([3, 41, 12, 2, 74, 15]) you check if the preset variable “smallest” is bigger than the current item (first item is 3 in this list). However, since the if statement is True in the first iteration (smallest is None), you set that smallest variable to the itervar (3 in this case) and then you put a “break” immediately after it. That causes you to break out of the whole for loop, essentially stopping it right after the first iteration (it only checked 3 here) and then going on with the next line of code which is not included in the for loop (which is the last line of code here, print(“Smallest:”, smallest). If you don’t put the break statement inside, the for loop will go through each item until the last one and then “break out of the loop” on its own so to say. That is why it’s called a definite loop, since it has a set number of times (in this case the length of the list) it iterates through a given iterable. If you put "print(“debug”) right before you set smallest = itervar, you can observe that the loop only executes one single time. Print statements are really helpful for cases where you don’t understand what’s going on. I hope I could help you!

could you provide screenshot?

I am sorry, i cannot give screenshot but the question is from loop idioms phython; thank you

Hello friend,

The for loop will break after 3.

for itervar in [3, 41, 12, 9, 74, 15]:

This will assign itervar value to 3 and evaluate the condition, in the given line:

if smallest is None or itervar < smallest:

If the first condition is true , then it’s true for ‘Or operator’, it will assign (smallest = 3) and break.

print("Loop:", itervar, smallest)

This print statement will not executed because it’s indented inside the for block and the loop was broken.

The output will be :

Before: None
Smallest: 3

I don’t know about the answer 6.

This will give you 6.

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)

Output:

Before: None
Loop: 6
Smallest: 3

I wish i can help you more.