CPTR 124 Fundamentals of Programming


In this lab you will work with Python strings.


Problem Statement

It is said that teletype operators, after establishing a connection, would type the line:

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

They used this line because it contains every letter of the alphabet. Write a program to test lines entered by the user to see if they have the same property.

When run, your program should request a line of input from the user and check to see if it contains all the letters of the English alphabet. A valid input line will consist of

Note that according to the rules above an otherwise valid sentence that ends with a period is not considered a valid line. When the user enters a line of text and presses enter, the program checks the line to see if it contains all the letters of the alphabet. The user may continue entering additional lines, and the program checks each line entered. The user enters a line consisting of a single period (.) when finished.

For each line entered, the program displays a report of the missing letters, if any. If the line used all the letters of the alphabet, the program prints "All letters present"; otherwise, it prints "Missing letters: " followed by the list of letters omitted from the line of text. The missing letters, if any, must appear in alphabetical order.

If the user supplies an invalid line, the program should print "Invalid line."

See the sample run below for examples of the exact presentation of the output.

Sample Run

Enter line to check (single "." terminates) NOW IS THE TIME FOR ALL GOOD MEN TO COME TO THE AID OF THEIR COUNTRY Missing letters: BJKPQVXZ Enter line to check (single "." terminates) THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG All letters present Enter line to check (single "." terminates) THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. Invalid line Enter line to check (single "." terminates) abc Invalid line Enter line to check (single "." terminates) ABC Missing letters: DEFGHIJKLMNOPQRSTUVWXYZ Enter line to check (single "." terminates) @#$ Invalid line Enter line to check (single "." terminates) .

The easiest way to keep track of the letters (one-character strings) in Python is to use a dictionary. Traverse the string, and keep track of the count of each letter. When finished traversing the string, check to see if each character is present in the dictionary. Report the missing letters.

Note: The character "A" has an ASCII code 65, "B" is 66, etc. Python's ord function reveals the ASCII value of a character. You can convert an integer ASCII code to a character using the chr function. With the chr function, we can iterate over all the letters of the alphabet as follows:

for i in range(26): print(chr(i + 65), end=' ') print()

This simple program prints

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You do not want to print out each letter, of course, but you can use a similar loop to query your dictionary to see if a letter is present (or maybe has a count greater than zero).

Check out

Your finished program will be evaluated for correctness and compliance. Once you have been checked out you may submit your code to eclass.e.southern.edu.