Difference between revisions of "CSC111 Lab 5 2015"
Line 6: | Line 6: | ||
=Preparation= | =Preparation= | ||
<br /> | <br /> | ||
− | * Copy paste the program below in a new Idle window, and call it '''lab5Companion.py'''. Save | + | * Copy paste the program below in a new Idle window, and call it '''lab5Companion.py'''. |
+ | * Save your new program to your working directory, where you normally save your lab programs. | ||
* Run the program. It should create 3 text files in your folder. You will be using them in this lab. The files are called: | * Run the program. It should create 3 text files in your folder. You will be using them in this lab. The files are called: | ||
** chocoQuotes.txt | ** chocoQuotes.txt | ||
Line 79: | Line 80: | ||
def main(): | def main(): | ||
+ | # make sure main knows that this variable exists | ||
+ | global text | ||
+ | |||
# split the long string into individual lines | # split the long string into individual lines | ||
lines = text.split( "\n" ) | lines = text.split( "\n" ) | ||
Line 90: | Line 94: | ||
<br /> | <br /> | ||
* Run the program. Verify that it outputs the entirety of the text. | * Run the program. Verify that it outputs the entirety of the text. | ||
− | * Just to make sure we understand what the variable '''lines''' contain, make your program print | + | * Just to make sure we understand what the variable '''lines''' contain, make your program print '''lines''', with a simple <tt>print( lines )</tt> statement. |
− | * Observe that it's just a Python '''list'''. | + | * Observe that it's just a regular Python '''list'''. |
+ | <br /> | ||
+ | < br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ===Challenge #1=== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * Modify your program, and make it print the poem so that each line is centered in a field of 60 spaces. You must use the '''text''' variable, and print its line. Do not create a new version of the poem! | ||
+ | * Expected output | ||
+ | <br /> | ||
+ | <source lang="text"> | ||
+ | Chocolate | ||
+ | Chocolate is the first luxury. | ||
+ | It has so many things wrapped up in it: | ||
+ | Deliciusness in the moment, | ||
+ | childhood memories, | ||
+ | and that grin-inducing | ||
+ | feeling of getting a reward for being good. | ||
+ | --Mariska Hargitay | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | < br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
− | === | + | ===Challenge #2=== |
+ | |} | ||
+ | [[Image:QuestionMark2.jpg|right|120px]] | ||
+ | * Make your program print the first line of the poem, centered, but also in uppercase. | ||
+ | * Make it display the other lines centered, without changing their case. | ||
+ | * Expected output | ||
+ | <br /> | ||
+ | <source lang="text"> | ||
+ | CHOCOLATE | ||
+ | Chocolate is the first luxury. | ||
+ | It has so many things wrapped up in it: | ||
+ | Deliciusness in the moment, | ||
+ | childhood memories, | ||
+ | and that grin-inducing | ||
+ | feeling of getting a reward for being good. | ||
+ | --Mariska Hargitay | ||
+ | </source> | ||
+ | <br /> | ||
Revision as of 16:52, 23 February 2015
--D. Thiebaut (talk) 12:17, 22 February 2015 (EST)
<showafterdate after="20150225 11:00" before="20150601 00:00">
Preparation
- Copy paste the program below in a new Idle window, and call it lab5Companion.py.
- Save your new program to your working directory, where you normally save your lab programs.
- Run the program. It should create 3 text files in your folder. You will be using them in this lab. The files are called:
- chocoQuotes.txt
- hargitay.txt
- joanneHarris.txt
# lab5Companion.py # D. Thiebaut # This program will create several files in the directory # where you will run it. # The files are retrieved from a Web server, stored temporarily # in a string, and stored in a local file on your disk. # The 3 files are very, very short. # Their names are: # - chocoQuotes.txt # - hargitay.txt # - joanneHarris.txt import urllib.request # the lib that handles the url stuff # main program def main(): # list of files to retrieve fileNames = [ "chocoQuotes.txt", "hargitay.txt", "joanneHarris.txt" ] # get each file, one at a time for fileName in fileNames: # url where the files are located url = "http://cs.smith.edu/~dthiebaut/111/files/" + fileName # fetch the file and put its contents in the # strign text. response = urllib.request.urlopen( url ) text = response.read().decode( 'UTF-8' ) # write text to file, in the same directory as this program. file = open( fileName, "w" ) file.write( text + "\n" ) file.close() # give feedback to user. print( "File {0:1} created in your folder".format( fileName ) ) main()
Splitting Long Strings
In this section you will see how to take text represented as a multi-line string, and process it various ways.
- Long strings that span several lines can be created by enclosing them in triple quotes; either triple single-quotes (), or triple double-quotes (""").
- Copy/paste the program below in a new program. You may call it lab5.py
# lab5.py text = """Chocolate Chocolate is the first luxury. It has so many things wrapped up in it: Deliciusness in the moment, childhood memories, and that grin-inducing feeling of getting a reward for being good. --Mariska Hargitay""" def main(): # make sure main knows that this variable exists global text # split the long string into individual lines lines = text.split( "\n" ) # print each line for line in lines: print( line ) main()
- Run the program. Verify that it outputs the entirety of the text.
- Just to make sure we understand what the variable lines contain, make your program print lines, with a simple print( lines ) statement.
- Observe that it's just a regular Python list.
< br />
Challenge #1 |
- Modify your program, and make it print the poem so that each line is centered in a field of 60 spaces. You must use the text variable, and print its line. Do not create a new version of the poem!
- Expected output
Chocolate
Chocolate is the first luxury.
It has so many things wrapped up in it:
Deliciusness in the moment,
childhood memories,
and that grin-inducing
feeling of getting a reward for being good.
--Mariska Hargitay
< br />
Challenge #2 |
- Make your program print the first line of the poem, centered, but also in uppercase.
- Make it display the other lines centered, without changing their case.
- Expected output
CHOCOLATE
Chocolate is the first luxury.
It has so many things wrapped up in it:
Deliciusness in the moment,
childhood memories,
and that grin-inducing
feeling of getting a reward for being good.
--Mariska Hargitay
</showafterdate>