Lab 5: Loops | CMSC 105 Elementary Programming - Fall 2024

Lab 5: Loops

Program 1: while loop

loop.py

''' 
Author name: <Your name here>
This program will print the first 50 even numbers starting from 2
'''

count = 2
while count <= 50:
    print(count)
    count = count + 2

Program 2: Names

Using a loop (for or while), write a program that reads 10 different names from a user. If the name contains the letter ‘e’, continue with next iteration. If it doesn’t contain the alphabet ‘e’, print the message “Name Accepted” and stop.

Hint: Use find function to search for a letter in the string.

Save the program is names.py and attach it to the Blackboard lab assignment.

names.py

''' 
Author name: <Your name here>
This program reads 10 different names from a user. If the name contains the letter 'e', continue with next iteration. If it doesn't contain the alphabet 'e', `print` the message "Name Accepted" and stop.
'''

# For ten times through the for loop
for i in range(10):
    # First read a name from the user
    name = input("Enter a name: ")
    # Convert the name to lower case for searching 
    name = name.lower()
    # Using the find function, 
    # check to see of there exists a letter 'e' 
    # in the name. If there is, then continue to the next name
    # else, print "Name Excepted" and break out of the for loop.
    if name.find("e") >= 0:
        continue
    else:
        print("Name Excepted")
        break

Program 3: for loop

countdown.py

''' 
Author name: <Your name here>
This program will print the numbers in reverse order from 100 to 1
'''

# For each i in the range from 100 to 1, print i
for i in range(100, 0, -1):
    print(i)