Introduction to Python
davidbpython.com
Project Discussion, Session 3
3.1 | Notes typing assignment. Please write out this week's transcription notes. |
3.2 | Take user input repeatedly until user enters a 4-digit string. |
This is a simple while True: loop that takes input with input(), then determines to break only if the input is 4 digits. Of course, there are two tests necessary to determine whether a string is 4 digits: whether its len() is 4, and whether it is all digits (which can be determined using .isdigit(). |
|
Some students choose to use two if statements, arranges as shwon:
while True: if year has len() of 4: if year is all digits: print thanks break |
|
However, it's better to use a compound test so a nested if statement is not necessary. This compound test uses 'and':
while True: take input for year if year has len() of 4 and year is all digits: print thanks break |
|
You could also use a compound test with 'or':
while True: take input for year if year is not len() of 4 or year is not all digits: print try again else print thanks break |
|
Keep in mind that you can put 'not' in front of any test to negate it -- with a compound test, however, each test must stand alone -- so if you're negating a test with 'not', it will apply only to the individual test (i.e., not both tests in a compound test). I am asking you to choose a compound test because it's inherently simpler, and because it allows you to practice compound tests. |
|
3.3 | Calculate the sum and count of Mkt-RF values (1st column of floats) for a given year. |
Start with a 4-digit string year (for example, '1990') assigned to a string variable: |
|
year = '1990'
|
|
Then looping through the FF_data.txt file, calculate the average MktRF value (the 1st column of floating-point values, i.e. the 2nd column in the file) for that year. I recommend an incremental approach:
|
|
3.4 | Next calculate an "average" value by dividing a float by an int. Round the result to two places so the result is 5.13. |
This is a simple division. It's been made a separate program so you'll understand that it does not belong inside a loop. It needs to be calculated only once. |
|
3.5 | Create a complete program built with the last 3 solutions, and test the entire program. |
PASTE IN THE CODE, ONE AFTER THE OTHER for the previous three assignments into one complete program, remove hard-coded values, and alter variable names so that they work together. (However please don't indent any one of the pasted solutions inside any of the others' blocks!) This is a complete program. You must test this entire program as shown below. If your previous three programs work as expected, it should be possible to combine them into one program without rewriting any code or "nesting" part of the code in another part. However, you will need to remove the hard-coded year, count and sum, any print statements from the first two solutions, and will probably also need to change variable names in one or more of the programs so that the three programs will be connected. Please do not "nest" one program into the code of another. You only need to paste the three solutions, in order, and "hook them up" by changing the variable names so that they match between parts. |
|
the input-taking program produces a 4-digit year the file parsing program takes a 4-digit year to calculate a count and sum the calculating program takes a count and sum to produce an average |
|
Of course you must test the overall program so that works as expected -- if it doesn't, you'll need to make sure that each program is producing the value as expected, and the the next program is using the value produced by the prior program. |
|
3.6 | Download and parse data from the internet. See homework assignment and best of luck! This is a fun one to complete. |