The focus of this week's lab is on:
if __name__ == '__main__ to organize your code.Create a Lab07 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)
Open VS Code. Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab07 folder.
Download digits.txt and place it in your lab folder.
Write code in a new source code file lab7_digits.py that reads in the data and stores it as one single-line string. When you print out the string, you should get:1 2 3 4 5 6 7 8 9 10 11 12
Try doing this two different ways:
+ operator)join methodHint: Each line ends with an invisible new line character. You probably want to discard each one. Recall: you can use either indexing or the strip string method to help with this.
Starting from your string from L2a, create a new string using both split and join methods (without using loops) that when printed, gives (again, not in a loop: print(yourString)):
1 2 3 4 5 6 7 8 9 10 11 12
Write your string from L1b to a new text file called digits2.txt. There should be one number per line of the file.
Look at the python documentation for an example of how to write a string to a file.
This can be done by creating a new file in write mode ("w")
newFile = open("digits2.txt", "w")
Use the file object's write method with a string as the argument, to add that string to your file.
Make sure to close the file using the close method.
Create a new file called lab7_letters.py. Download abcs.txt and place it in your lab folder.
Open up the text file in VS Code. For each of the 26 letters in the alphabet, there is a phrase for each of the 26 letters of the alphabet. For example:
a is for apple
b is for banana
Your goal is to create a function called decryption_table which creates a list of lists and returns it. Each sublist should contain:
'a', 'b', etc.'apple', 'banana', etc.Your list of lists should have 26 items.
After you create your function decryption_table which returns the list of lists, create a test function table_test that runs the following test code. Running lab7_letters.py should produce the expected output below:
letters=decryption_table() print(len(letters)) # 26 print(letters[0]) # ['a', 'apple'] print(letters[1][0]) # b print(letters[-1][1]) # zebra
Hints:
Now that you have your list of lists set up, your next goal is to create a function abcs which creates a single line string with the 26 letters of the alphabet (all next to each other, without spaces) using your list of lists and prints it.
Calling your function abcs should yield:
abcdefghijklmnopqrstuvwxyz
Write a function decrypt that takes in the parameter message, a string, and prints the message decoded.
After you create your function decrypt, create a test function decryption_test that runs the following test code.
decrypt('happy everyone lamb lamb open') # hello decrypt('water open rooster lamb donut') # world
Call your test function in your main guard (if __name__ == '__main__). Running lab7_letters.py should produce the expected output below:
hello world
Upload and submit your lab7_letters.py file on Google Classroom.
© 2021 Max Bender