Q:

Display numbers from a list using loop using python programming

belongs to collection: Python Loop Exercises

0

Display numbers from a list using loop

Write a program to display only those numbers from a list that satisfy the following conditions

  • The number must be divisible by five
  • If the number is greater than 150, then skip it and move to the next number
  • If the number is greater than 500, then stop the loop

Given:

numbers = [12, 75, 150, 180, 145, 525, 50]

Expected output:

75
150
145

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Use for loop to iterate each item of a list
  • Use break statement to break the loop if the current number is greater than 500
  • use continue statement move to next number if the current number is greater than 150
  • Use number % 5 == 0 condition to check if number is divisible by 5

Solution:

numbers = [12, 75, 150, 180, 145, 525, 50]
# iterate each item of a list
for item in numbers:
    if item > 500:
        break
    elif item > 150:
        continue
    # check if number is divisible by 5
    elif item % 5 == 0:
        print(item)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to count the total number o... >>
<< Write a python program to accept a number from a u...