A201 Introduction to Programming 1

A201 Lab 6

Date: Tuesday, September 26, 2023.
Due Date: Tuesday, October 3, 2023.

Goal: to use for loops with a sequence in Python.

In this lab, we will be working on the for loop in Python using sequences.

Open the Python Idle and create a new file called lab6.py. Add your name and the lab number as a comment at the top.

Ex. 1. Taking attendance

Write a Python program that takes attendance in a class. For that, declare a variable that stores a tupple where you write the names of everyone attending a meeting as a string. Then write a for loop that uses this sequence to print out each of the names as present. At the end, output a message stating how many people are present at this meeting. For that, use a counter to count how many iterations of the loop were made.

Test the program to make sure it works.

Ex. 2. a Loop over a string

Copy the following code discussed in class to the file lab6.py:

# program counting the letters in a string
print("Enter a string")
text = input()
letters = 0
print("the string has a length of", len(text))
for ch in text:
    if 'a' <= ch and ch <= 'z' or 'A' <= ch and ch <= 'Z':
        letters += 1
print("the string has", letters, "letters")

Test the program to see that it works fine.

b. Modify the program so that it counts the uppercase letters, lowercase letters, and digits separately and prints out each of their values.

Lab Submission

Upload the file lab6.py in Canvas, Assignments, Homework 6. You can wait until you finish the homework to upload both files at the same time.