Home > Back-end >  Why does the code not show the numbers that are less than 15? Break loop
Why does the code not show the numbers that are less than 15? Break loop

Time:11-12

a program that will iterates over each number in a the list then the if statement will check whether the iteration/number is greater than 15 then the loop will stop, otherwise the number from the list will be printed. List = [1, 4, 7, 8, 15, 20, 35, 45, 55]

List = [1, 4, 7, 8, 15, 20, 35, 45, 55]
for i in List:
    if i > 15:
        break
    elif i > 1:
        continue
    print(i)

CodePudding user response:

List = [1, 4, 7, 8, 15, 20, 35, 45, 55]
for i in List:
#print(i)
if i > 15:
    break
elif i > 1:
    pass
print(i)

CodePudding user response:

Place your print statement inside the elif check.

List = [1, 4, 7, 8, 15, 20, 35, 45, 55]
for i in List:
    if i >= 15:
        break
    elif i > 1:
        print(i)
  • Related