CS 115 Lab 7, Part A: Find $1 words

[Back to lab instructions]


Background

Computers internally store all data as sequences of 1s and 0s that can be interpreted as numbers. Therefore, people have agreed on a method for representing text in numeric form. The most common method is a system called Unicode. Using this table, you can see the Unicode representations of some common characters. The "Dec" column gives you each character's representation as a normal (base 10) number.

Python can show you the numeric representation of your text characters. If you have a character c, then ord(c) is the Unicode representation of that number.

Python can go the other direction, too; if you have a number i, then chr(i) is the character represented by that number.

Using the Online Python Tutor, and referencing the Unicode table, answer Question 18 in your writeup.

Specification

Before you start programming, read this specification to understand what your final code should do.

Your program should:

Sample output #1:

Enter a word: QuiT

or

Sample output #2:

Enter a word: CoFFee
Your word is worth $0.40.
Enter a word: accuMulate
Your word is worth $1.00.
Congratulations!
Enter a word: QUIT

Instructions

  1. Create a new Python source code file called lab07a.py:
    """
    Program: CS 115 Lab 7a
    Author: Your name
    Description: This program finds $1.00 words.
    """
    
    
    def main():
    
        # Ask the user for a word, and save the word to a variable.
        # Convert the user's word to lowercase
        # As long as the user's word is not 'quit'...
            # Echo their word back to them
            # Get a new word from them, convert it to lowercase,
            # and save it to the same variable.
        
    
    main()

    Start by translating the pseudocode to Python that will repeatedly ask the user for a word and convert it to lowercase until the user enters quit. For now, you will just repeat that word back to them.

    Sample input/output:

    Enter a word: QuiT
    
    or
    Enter a word: CoFFEE
    coffee
    Enter a word: teA
    tea
    Enter a word: QUIT
  2. Modify your program so that instead of printing the user's word, it prints each of the characters in the lowercase version of the word on a separate line (see below).

    This will require nesting a for loop inside your while loop – your while loop performs one iteration per word, and the for loop will perform one iteration per letter within the word.

    Sample input/output:

    Enter a word: CoFFEE
    c
    o
    f
    f
    e
    e
    Enter a word: teA
    t
    e
    a
    Enter a word: quit
  3. Modify your program so that it prints the numeric Unicode value for each character instead of the character itself. Recall that for a character variable c, you can get its Unicode value with ord(c).

    Sample input/output:

    Enter a word: CoFFEE
    99
    111
    102
    102
    101
    101
    Enter a word: teA
    116
    101
    97
    Enter a word: quit
    
  4. In order to compute the "value" of a word according to our point system, we want the letter 'a' to be worth 1 point, 'b' to be worth 2 points, etc. However, we can see in our output that the Unicode value of 'a' is 97, 'c' is 99, etc. Answer Question 19 in your writeup.
  5. Based on your answers, figure out how you can adjust this output to print 1 for 'a', 2 for 'b', etc.

    Sample input/output:

    Enter a word: CoFFEE
    3
    15
    6
    6
    5
    5
    Enter a word: teA
    20
    5
    1
    Enter a word: quit
    
  6. Instead of printing the value of each character, add up the values of the characters in each word to produce a total. Print the total once per word.

    Sample input/output:

    Enter a word: CoFFEE
    Total: 40
    Enter a word: teA
    Total: 26
    Enter a word: quit
    
  7. Modify the print statements so that they match the specification. You can format a numeric variable x to two decimal places like this:

    "{0:.2f}".format(x)

    Sample input/output:

    Enter a word: CoFFEE
    Your word is worth $0.40.
    Enter a word: teA
    Your word is worth $0.26.
    Enter a word: quit
  8. Finally, add the congratulations for $1.00 words:
    Enter a word: CoFFEE
    Your word is worth $0.40.
    Enter a word: accumulate
    Your word is worth $1.00.
    Congratulations!
    Enter a word: quit
  9. When your code matches the examples, call an instructor to demo.
  10. Continue to Part B.