Lab Dates: Wednesday-Thursday, 13-14 March 2013

Today's lab focuses on simple string manipulation from Chapter 5.

Python Challenge For today's lab, we are going to work through the first two problems of the Python Challenge, which is a series of programming exercises. The solution to each provides the key to start the next one.

For the first challenge, you do not need to define a function, only use the python shell to evaluate the mathematical expression. Take the number and use it as the URL to go to the next challenge. Make sure to add '.html' to your answer.

For the second challenge, let us first look at the Python program text2numbers.py.

# text2numbers.py
#     A program to convert a textual message into a sequence of
#         numbers, utlilizing the underlying Unicode encoding.

def main():
    print("This program converts a textual message into a sequence")
    print ("of numbers representing the Unicode encoding of the message.\n")
    
    # Get the message to encode
    message = input("Please enter the message to encode: ")

    print("\nHere are the Unicode codes:")

    # Loop through the message and print out the Unicode values
    for ch in message:
        print(ord(ch),  end=" ")
        
    print()

main()

Try the program with the string given in the second challenge. You will see all the numberic codes corresponding to the characters of the encoded message

Now we will decode the message by using the Python program decodeMessage.py.

# decodeMessage.py
#     A program to decode the Python Challenge (2) message.
#     Each character must be mapped to the character 2 places ahead.

def main():
    print("This program decodes a textual message")
    print("Each character is replaced with the character 2 places ahead in the alphabet.")
    print("The characters \"\'\", \".\", \"(\", \")\", and \" \" are not decoded.\n")  
    
    # Get the message to encode
    message = input("Please enter the message to decode: ")

    print("\nHere is the decoded message:")

    # decode the message 1 character at a time
    decodedMessage = ""
    for ch in message:
        if (ch == '.') or (ch == "'" ) or (ch == '(') or (ch == ')') or (ch == ' '):
            decodedMessage = decodedMessage + ch
        else:
            codeNumber = ord('a') + ((ord(ch) - ord('a') + 2) % 26)
            decodedMessage = decodedMessage + chr(codeNumber) 
        
    print("\nThe decoded message is:", decodedMessage)
    print()

main()

After completing your program, go to Labs in Blackboard and click on the link: Lab 5. Take the 5 question test about the lab. Remember to click the Submit button before closing the lab exercise. You may take the test as many times as you want until you get a perfect score.