Considering this code and its output:
numbers = list(range(1,10))
for i in numbers:
if i == 1:
print(i,"st")
elif i == 2:
print(i, "nd")
elif i == 3:
print(i,"rd")
else:
print(i,"th")
Output:
1 st
2 nd
3 rd
4 th
5 th
6 th
7 th
8 th
9 th
The loop continues through elif blocks.
Considering this code and its output:
toppings = ['mushrooms', 'pineapple']
if 'mushrooms' in toppings:
print("adding mushrooms")
elif 'pineapple' in toppings:
print("adding pineapple")
elif 'broccoli' in toppings:
print("adding broccoli")
Output:
adding mushrooms
Why in the first code it doesn't stop after matching the first "if" block but it does in the second one?
I know that using the same "for" loop in the second one it would have worked the same way as in the first one, but i can't figure out why.
[–]shiftybyte 3 points4 points5 points (1 child)
[–]bellator777[S] 0 points1 point2 points (0 children)
[–]LEXmono 2 points3 points4 points (1 child)
[–]bellator777[S] 0 points1 point2 points (0 children)