Skip to content
Laviyx Learn

Try

Python Loops Practice

Practise for loops, while loops, range(), break and continue.

Beginner3 problemsUpdated

Review first: Loops: for and while, Lists

  1. 1.Count the vowels

    Beginner

    Count how many vowels (a, e, i, o, u) are in the word "programming" and print the count.

    Show hint

    Loop over the string and check if letter in "aeiou".

    Show solution
    Python
    word = "programming"
    vowels = 0
    
    for letter in word:
        if letter in "aeiou":
            vowels += 1
    
    print(vowels)
    Output
    3
  2. 2.Countdown

    Beginner

    Use a while loop to print a countdown from 5 to 1, then print Lift off!.

    Show hint

    Start a variable at 5, loop while it is greater than 0, and subtract 1 each time.

    Show solution
    Python
    n = 5
    
    while n > 0:
        print(n)
        n -= 1
    
    print("Lift off!")
    Output
    5
    4
    3
    2
    1
    Lift off!
  3. 3.Stop at the first negative

    Beginner

    Given readings = [12, 7, 0, 15, -3, 8], add up the readings until you reach the first negative number, skipping zeros. Print the total.

    Show hint

    Use continue to skip zeros and break when a reading is below zero.

    Show solution
    Python
    readings = [12, 7, 0, 15, -3, 8]
    total = 0
    
    for r in readings:
        if r == 0:
            continue
        if r < 0:
            break
        total += r
    
    print(total)
    Output
    34

    12 + 7 + 15 = 34. The zero is skipped by continue, and the loop stops at -3, so 8 is never added.