General Notes

Submit the following programs via Blackboard:

  1. Due Date: 31 January Reading: Chapters 1 & 2
    Write a program that prints "I love python" to the screen.
  2. Due Date: 3 February Reading: Chapters 1 & 2
    Write a program that prints your name to the screen 15 times.
  3. Due Date: 4 February Reading: Chapters 1 & 2
    Modify the chaos program (available from the author's website) so that it prints out 15 values instead of 10.
  4. Due Date: 5 February Reading: Chapters 1 & 2
    Write a program that prints out the first 10 numbers.
    (Hint: see the sample program on p 37 of Zelle's Chapter 2 lecture notes-- available from his website.)
  5. Due Date: 6 February Reading: Chapters 1 & 2
    Write a program that prints out the first 20 even numbers.
    (Hint: see the sample program on p 37 of Zelle's Chapter 2 lecture notes-- available from his website.)
  6. Due Date: 10 February Reading: Chapters 1 & 2
    Modify the avg2.py program (Section 2.5.3 from the book and from the book's webpage) to find the average of three exam scores.
  7. Due Date: 11 February Reading: Chapters 1 & 2
    Write a program that will print out a conversion table for dollars and another currency. The currency you should use is based on the first letter of your first name:
    If your name begins with...Use the currency:$1 is worth:
    AAfghan Afghani (AFN)51.08
    BBangladeshi Taka (BDT)80.58
    CCosta Rican Colon (CRC)499.38
    DDanish Krone (DKK)5.67
    EEuro (EUR)0.77
    FFalkland Island Pound (FKP) 0.62
    GGuatemalan Quetzal (GTQ)0.13
    HHungarian Forint (HUF) 0.00456
    IIndonesian Rupiah (IDR)9639.99
    JJapanese Yen (JPY)83.85
    KKenyan Shilling (KES)0.0116
    LLebanese Pound (LPP)0.000664
    MMoroccan Dirham (MAD)0.118
    NNepalese Rupee (NPR)0.0115
    OOmani Rial (OMR)0.384
    PPolish Zloty3.11
    QQatari Riyal (QAR)0.275
    RRussian Ruble (RUB)30.80
    SSomali Shilling (SOS)0.000619
    TThai Baht (THB)30.59
    UUkrainian Hryvna (UAH)8.10
    VVenezuelan Bolivar (VEH)4.30
    WSamoan Tala (WST)2.27
    XEast Caribbean Dollar (XCD)2.70
    YYemeni Rial (YER)0.00477
    ZZimbabwean Dollar (ZWD)361.90
    For example, if your first name is Eric, you would use Euros. From the table above, we have that one dollar is worth 0.77 euros. You should begin the program by printing out your name. On each line, your program should write the number of dollars and the corresponding number of your currency. For example:
    Eric's converting program
    		
    Dollars:    Euros:
    1           0.77
    2           1.53
    3           2.3
    4           3.07
    5           3.84
    		
    Your program should print the information for 1, 2, ..., 10 dollars. You do not need to worry about formatting (we will talk about that more in Chapter 5), but you do need to calculate all 10 entries. Hint: modify the program from the Lab 2.
  8. Due Date: 13 February Reading: Chapter 3
    Write a program that asks the user for distance in feet and prints the number of yards and feet (assume that there are 3 feet to the yard). For example, if the user entered, 10, your program would output, 3 yards 1 feet. Note that you should use "yards" and "feet" for all inputs (even in the cases where it is grammatically incorrect).
  9. Due Date: 14 February Reading: Chapter 3
    Write a program that asks the user for the number of roses they have ordered for Valentine's Day, and prints the number of dozens of roses and single roses. For example, if the user entered, 26, your program would output, 2 dozen and 2 roses. Note that you should use "dozen" and "roses" for all inputs (even in the cases where it is grammatically incorrect).
  10. Due Date: 18 February Reading: Chapter 3
    Write a program that asks the user for time in days and prints the number of years and days (assume that it is not a leap year and there are 365 days in a year). For example, if the user entered, 400, your program would output, 1 years 35 days. Note that you should use "years" and "days" for all inputs (even in the cases where it is grammatically incorrect).
  11. Due Date: 19 February Reading: Chapter 3
    Built into python is a module for simple drawing, called Turtle graphics (see Lab 3). To use the Turtle graphics module, you must import it, just like with the math module. Some basic turtle commands are:
    • left(degs): turns your turtle left degs degrees
    • right(degs): turns your turtle left degs degrees
    • forward(s): moves your turtle forward s steps
    • backward(s): moves your turtle forward s steps
    The following draws a 4-sided figure or square:
    import turtle
    
    def main():
        daniel = turtle.Turtle()    #Set up a turtle named "daniel"
        myWin = turtle.Screen()     #The graphics window
    
        #Draw a square
        for i in range(4):
            daniel.forward(100)     #Move forward 10 steps
            daniel.right(90)        #Turn 90 degrees to the right
    
        myWin.exitonclick()         #Close the window when clicked
        
    main()		
    		
    Modify this program to draw a 6-sided figure or hexagon. Make sure to include the standard introductory comments at the beginning of your program as well as to change the name of the turtle to your name.
  12. Due Date: 20 February Reading: Chapter 3
    Modify the turtle program to draw two squares to the screen. Make sure to include the standard introductory comments at the beginning of your program as well as to change the name of the turtle to your name.
  13. Due Date: 21 February Reading: Chapter 3
    Write a program to calculate how much money you would have if saved $10 a month in your change jar. Since you are not using a bank, no interest is paid on your savings. Your program should ask the user for the number of days and display the amount of money for each day. The starting amount of money is $0. Here is a sample interaction:
    Please enter the number of days:  4
    Month 1:  $10
    Month 2:  $20
    Month 3:  $30
    Month 4:  $40
    		

  14. Due Date: 24 February Reading: Chapter 3
    Write a program to calculate how much money you would have if your money doubled every day. Your program should ask the user for the number of days and display the amount of money for each day. The starting amount of money is $1. Here is a sample interaction:
    Please enter the number of days:  8
    Day 1:  $1
    Day 2:  $2
    Day 3:  $4
    Day 4:  $8
    Day 5:  $16
    Day 6:  $32
    Day 7:  $64
    Day 8:  $128
    		
  15. Due Date: 25 February Reading: Chapter 3
    Write a program that asks the user for an amount and calculates the total after applying the New York City tax rate of 8.875 percent. For example, if the user enters 1000, your program should print $1088.75 (do not worry about formatting the number to have only two digits after the decimal-- we will discuss how to do that in Chapter 5).
  16. Due Date: 26 February Reading: Chapter 3
    Problem #5, p 76: The Konditorei coffee shop sells coffee at $10.50 a pound plus the cost of shipping. Each order ships for $0.86 per pound + $1.50 fixed cost overhead. Write a program that asks the user for the number of pounds of coffee and calculate the cost of an order.
  17. Due Date: 27 February Reading: Chapter 3
    Write a program that will calculate the cost per ride on an unlimited ride MetroCard. Your program should ask the user for two numbers: the cost of the card, and the number of rides. The program should then print the cost per each ride. For example, if the user paid $30 for the card and took 25 trips, your program should print out the cost per ride of $1.20.
  18. Due Date: 3 March Reading: Chapter 3
    Write a program that will calculate the average miles per hour for a trip. Your program should ask the user for two numbers: the miles traveled, and the number of hours the trip took. The program should then print the average miles per hour. For example, if the user traveled 10 miles in 2 hours, your program should print out that they averaged 5 miles per hour.
  19. Due Date: 4 March Reading: Chapter 5
    Write a program that asks the user for a line of text and prints out the same line with every character in upper case. For example:
    Please enter a line:  I love python
    I LOVE PYTHON
    
  20. Due Date: 5 March Reading: Chapter 5
    Write a program that asks the user for a line of text and prints the number of characters entered:
    Please enter a line:  I love python
    You entered 13 characters.
    
  21. Due Date: 7 March Reading: Chapter 5
    Write a program that measures strings in terms of the length of your name. For example, if your first name is "Kate", you would use the length of the string "Kate" which is 4. If your name is "Daniel", you would use the length 6.

    Your program should print out your name to the screen and then ask the user to enter a string. You should then print out how long the string is in terms of the length of your name (that is, the length of the user's string divided by your length). For example,

    The measuring string is "Kate"
    Please enter a string:  Hello world
    Your string is 2.25 Kate's long.
    		
    While, if your name was Daniel, your program would look like:
    The measuring string is "Daniel"
    Please enter a string:  Hello world
    Your string is 1.8333 Daniel's long.
    		

  22. Due Date: 10 March Reading: Chapter 5
    Modify the encode.py program from Lab 5 to use an offset entered by the user to encrypt the message. The program in the lab encrypts each character by the character 1 place ahead in the alphabet. Your program should ask the user for the offset and encrypt their string by replacing each character by one that is offset places ahead in the alphabet.
  23. Due Date: 11 March Reading: Chapter 5
    Problem #9, p 163: Write a program that prints out the number of words in a sentence entered by the user.
    (Hint: how can you tell when a word ends? Count those and adjust slightly to get a count on the number of words.)
  24. Due Date: 12 March Reading: Chapter 5
    Problem #10, p 163: Write a program that prints out the average word length in a sentence entered by the user.
  25. Due Date: 14 March Reading: Chapter 5
    Write a program that asks the user for a list of prices of items purchased and prints out each price and the total, formatted nicely. Here is a sample run of the program:
    Please enter the prices:  2.34, .99, 100, 81.05, 90
    		
    Your receipt:
             2.34
             0.99
           100.00
            81.05
            90.00
    ----------------
    Total: 274.38	
    		
    (Hint: use the format() statement discussed in Chapter 5.)
  26. Due Date: 17 March Reading: Chapter 5
    Write a program that asks the user for a name of a file and then prints to the screen the number of characters and lines in the file. Hint: see Lab 6.
  27. Due Date: 19 March Reading: Chapter 5
    Write a program that asks the user for the name of a file and a line length. Your program should then "wrap" all lines that are longer than line length, and print the result on the screen. For example, if the file contains:
    01234567890123456789012345
    This line has more than 20 characters.
    This one has less 
    And this one has lots, lots, lots, more than 20 characters!
    		
    and the user entered the length of 20, all lines longer than 20 would be wrapped to the next line:
    01234567890123456789
    012345	
    This line has more t
    han 20 characters.
    This one has less 
    And this one has lot
    s, lots, lots, more than 20 characters!
    		
    Hint: break the problem in to parts: first write a program that will print lines from a file to the screen (see Lab 6). Then modify your initial program to only print lines up to the length entered. And, to finish the program, then add in the code that prints lines that are longer than the length entered.
  28. Due Date: 21 March Reading: Chapter 5
    Write a program that asks for a list of names and a list of cities, as well as a file. Your program should use the file as a template to generate letters customized with the names and cities entered. You should generate a letter for each name/address pair on the inputted lists. The customized letters should replace every the **INSERT NAME HERE** with the name and **INSERT ADDRESS HERE** with the address.

    For example, if the file inputTemplate.txt contained:

    						New York, New York
    						11 March 2013
    	
    **INSERT NAME HERE**
    **INSERT ADDRESS HERE**
    
    Dear **INSERT NAME HERE**,
    
    Thank you for your service to New York City, and,
    in particular, to the education of its residents.
    Those in **INSERT ADDRESS HERE** appreciate it!
    
    Best wishes to **INSERT NAME HERE** and your family,
    
    	--CUNY
    		

    A sample run of the program would be:

    Please enter the name of the template file:  inputTemplate.txt
    Please enter names of recipients:  Herbert H. Lehman, Bernard M. Baruch, Fiorello H. LaGuardia
    Please list addresses:  Bronx NY, New York NY, Queens NY 
    		
    Your customized letters are below:
    		
    						New York, New York
    						11 March 2013
    	
    Herbert H. Lehman
    Bronx NY
    
    Dear Herbert H. Lehman,
    
    Thank you for your service to New York City, and,
    in particular, to the education of its residents.
    Those in Bronx NY appreciate it!
    
    Best wishes to Herbert H. Lehman and your family,
    
    	--CUNY
    	
    	
    	
    
    						New York, New York
    						11 March 2013
    	
    Bernard M. Baruch
    New York NY
    
    Dear Bernard M. Baruch,
    
    Thank you for your service to New York City, and,
    in particular, to the education of its residents.
    Those in New York NY appreciate it!
    
    Best wishes to Bernard M. Baruch and your family,
    
    	--CUNY
    	
    
    
    	
    						New York, New York
    						11 March 2013
    	
    Fiorello H. LaGuardia
    Queens NY
    
    Dear Fiorello H. LaGuardia,
    
    Thank you for your service to New York City, and,
    in particular, to the education of its residents.
    Those in Queens NY appreciate it!
    
    Best wishes to Fiorello H. LaGuardia and your family,
    
    	--CUNY	
    		
    		

  29. Due Date: 24 March Reading: Chapter 5
    Modify the program from Lab 7 to include addresses in the file. The database has an additional attribute, address, and information can be included using the following format:
    insert into customer (first, last, address) values ('FirstName', 'LastName', 'Address')
    A sample input file is mayorsAddresses.txt.
  30. Due Date: 25 March Reading: Chapter 5
    Modify the program from Lab 7 to input a comma separated value (CSV) file and output a file where all fields are separated by tabs. That is, replace all commas in the file by TABs.
    (Hint: See the list of string functions on p 140 of the textbook.)
  31. Due Date: 26 March Reading: Chapter 5
    Write a program that asks the user for a file containing a list of items to be completed and a name for an output file. Your program should then write the list, with item numbers to the output file. For example, if the input file is:
    Finish my python homework.
    Buy milk.
    Do laundry.
    Update webpage.
    
    Then the output file would be:
    1.  Finish my python homework.
    2.  Buy milk.
    3.  Do laundry.
    4.  Update webpage.
    
  32. Due Date: 27 March Reading: Chapter 6
    (From Chapter 6, #1, Page 196) Write a program to print the lyrics of the song "Old MacDonald." Your program should print the lyrics for five different animals, similar to the example verse below.
    	   Old MacDonald had a farm, Ei-igh, Ee-igh, Oh!
    	   And on that farm he had a cow, Ee-igh, Ee-igh, Oh!
    	   Whith a moo, moo here and a moo, moo there.
    	   Here a moo, there a moo, everywhere a moo, moo.
    	   Old MacDonald had a farm, Ei-igh, Ee-igh, Oh!
    	
    (Hint: use a function with two input parameters one for the animal and the other for the related sound)
  33. Due Date: 31 March Reading: Chapter 6
    Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings. That is, if the input parameter is ["Daniel","Nashyl","Orla", "Simone","Zakaria"], your function should return [6, 6, 4, 6, 7]. The file you submit should include a main() function that demonstrates that your function works.
  34. Due Date: 1 April Reading: Chapter 6
    Write a function that takes as a parameter a list of strings and returns a list containing the first letter of each of the strings. That is, if the input parameter is ["Daniel","Nashyl","Orla", "Simone","Zakaria"], your function should return ['D', 'N', 'O', 'S', 'Z']. The file you submit should include a main() function that demonstrates that your function works.
  35. Due Date: 2 April Reading: Chapter 6
    Write a function that takes as a parameter a list of strings and returns a list containing the last letter of each of the strings. That is, if the input parameter is ["Daniel","Nashyl","Orla", "Simone","Zakaria"], your function should return ['l', 'l', 'a', 'e', 'a']. The file you submit should include a main() function that demonstrates that your function works.
  36. Due Date: 3 April Reading: Chapter 6
    Write a function that uses turtle graphics to draw a petal. Your function should take a turtle as its input parameter and draw a single petal. For example,

    Draw a flower by repeatedly calling your function. Your flower should have at least 10 petals. For example,

    (Note: you can change the color that your turtle, using the function, color(). For example, if you turtle is called flower to change it's color to purple, write flower.color("purple").)


  37. Due Date: 7 April Reading: Chapter 6
    Following Lab 8, fill in the missing function definitions for this program:
    def main():
    	welcome()             #Prints "Hello, world" to the screen
    	x,y = userInput()     #Asks user for 2 inputs and returns numbers entered
    	d = calculate(x,y)    #Returns the difference of the parameters
    	displayResults(x,y,d) #Prints the two inputs, and d
    main()
    
    		
    That is, write the functions welcome(), userInput(), calculate() and displayResults(). Submit a .py file containing all four functions you wrote, in addition to the main() function above.
  38. Due Date: 8 April Reading: Chapter 6
    Following Lab 8, fill in the missing function definitions for this program:
    def main():
        welcome()             #Prints "Welcome" to the screen
        age = userInput()     #Ask the user for their age and return number entered
        y = calculate(age)    #Using age, calculates year born
        displayResults(age,y) #Prints age and birth year
    
    main()
      		
    (That is, write the functions welcome(), userInput(), calculate() and displayResults().) Submit a .py file containing all four functions you wrote, in addition to the main() function above.
  39. Due Date: 9 April Reading: Chapter 6
    Following Lab 8, fill in the missing function definitions for this program:
    def main():
        welcome()             #Prints "Welcome" to the screen
        age = userInput()     #Ask the user for their age and return number entered
        y = retire(age)       #Using age, calculates retirement year (year turns 65)
        displayResults(age,y) #Prints age and retirement year
    
    main()
      		
    (That is, write the functions welcome(), userInput(), retire() and displayResults().) Submit a .py file containing all four functions you wrote, in addition to the main() function above.
  40. Due Date: 10 April Reading: Chapter 7
    Write a program asks the user for their birthdate (MM/DD/YYYY format) and prints out if the user is older than 18 and also if the user is over 21 as of April 20th, 2012.
  41. Due Date: 24 April Reading: Chapter 7
    Write a function that takes as two parameters: the zone and the direction, and returns the Bay Area Rapid Transit (BART) fare.
    • If the zone is 2 or smaller, the fare is 1.75.
    • If the zone is 3 and the direction is "outbound," the fare is 2.15.
    • If the zone is 3 and the direction is "inbound," the fare is 2.45.
    • If the zone is 4 and the direction is "outbound," the fare is 2.65.
    • If the zone is 4 and the direction is "inbound,"the fare is 2.95.
    • If the zone is greater than 4, return a negative number (since your calculator does not handle inputs that high).

    You should include in the file a main() that calls your function several times to demonstrate that it works.

  42. Due Date: 25 April Reading: Chapter 7
    Write a function that takes as two parameters: the zone and the ticket type, and returns the Copenhagen Transit fare.
    • If the zone is 2 or smaller and the ticket type is "adult," the fare is 23.
    • If the zone is 2 or smaller and the ticket type is "child," the fare is 11.5.
    • If the zone is 3 and the ticket type is "adult," the fare is 34.5.
    • If the zone is 3 or 4 and the ticket type is "child," the fare is 23.
    • If the zone is 4 and the ticket type is "adult," the fare is 46.
    • If the zone is greater than 4, return a negative number (since your calculator does not handle inputs that high).

    You should include in the file a main() that calls your function several times to demonstrate that it works.


  43. Due Date: 28 April Reading: Chapter 7
    Write a function that takes number between 1 and 7 as a parameter and prints out the corresponding day as a string. For example, if the parameter is 1, your function should print out Monday. If the parameter is 2, your function should print out Tuesday, etc. Include a main() function that demonstrates that your function works.
  44. Due Date: 29 April Reading: Chapter 7
    Write a function that takes number between 1 and 12 as a parameter and prints out the corresponding month as a string. For example, if the parameter is 1, your function should print out January. If the parameter is 2, your function should print out February, etc. Include a main() function that demonstrates that your function works.
  45. Due Date: 6 May Reading: Chapter 8
    Write a program that asks the user to enter a number between -10 and 10. If they enter a number out of range, print a message that the number is out of range and prompt them again for a number between -10 and 10. When the user enters a number in range, print the number to the screen and end the program.
  46. Due Date: 7 May Reading: Chapter 8
    Write a program that asks the user to enter a number between 1900 and 2012. If they enter a number out of range, print a message that the number is out of range and prompt them again for a number between 1900 and 2012. When the user enters a number in range, print the number to the screen and end the program.
  47. Due Date: 9 May Reading: Chapter 8
    Write a program that reads in a text file (name specified by the user) and prints out all the lines in the file to the screen until it encounters a line with more than 80 characters. Once it finds a long line, the program stops and prints out a message that lines must be shorter than 80 characters to be displayed.
  48. Due Date: 13 May Reading: Chapter 8
    Write a program that reads in a text file (name specified by the user) and prints out all the lines in the file to the screen until it encounters a line with more than 80 characters. Once it finds a long line, the program stops and prints out a message that lines must be shorter than 80 characters to be displayed.
  49. Due Date: 14 May Reading: Chapter 8
    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 5 characters. Once it finds a short line (one with fewer than 4 characters), the program stops.
  50. Due Date: 15 May Reading: Chapter 13
    Modify the selectionSort (from Chapter 13 of the book or Zelle's website) to sort lists of numbers backwards. For example, the list, [125,4,145,59,168,34,14,42], should be sorted as: [168,145,125,59,42,34,14,4].

    In your submitted file, include a main() function that demonstrates that the sort algorithm works.


  51. Due Date: 16 May
    This is the make-up problem for #28. The score from the problem below can replace your grade for #28.

    For this problem, you will read and graph values from a CSV file. A CSV file is a "Comma-Separated File," in which the values on each line are separated from each other by commas. Problem 28 used an example of a CSV file. This kind of file is used to represent data from all sorts of activities, to be downloaded from the Web.

    Your file will contain labels for the x axis in the first row and labels for the y axis in the first column. All other values will be numbers, which may or may not be contained in quotes.

    Write a function that accepts a line from a CSV file and returns a list of numbers. The numbers may or may not be contained in single or double quotes. This function should work for any CSV file in which the first column contains labels and the rest contain only numbers.

    The sample file we are providing represents sectors of the gross domestic product from the first quarter of 2002 to the last quarter of 2012. Each line of the file represents one sector of the economy (Consumption, Investment, Net Exports, Government, and Residual). The first row of the file contains labels for the time periods (I2002 for first quarter of 2002, II2002 for second quarter of 2002, etc.), the first column contains labels for the sectors.

    Next, write a main program that uses the function to create several graphs, each graph representing a single line of the CSV file. Numeric values appear in row 2, column 2 through the last row and last column, but they may be enclosed in single quotes or double quotes.

    For example, if you read the file:

    Time period,  I2002, II2002,III2002, IV2002,  I2003, II2003,III2003, IV2003
    GDP,11467.1,11528.1,11586.6,11590.6,11638.9,11737.5,11930.7,12038.6
    Consumption,7953.7,7994.1,8048.3,8076.9,8117.7,8198.1,8308.5,8353.7
    Investment,1781.9,1803.4,1808,1808.3,1810.4,1821.8,1888.4,1959.9
    Exports,-510.2,-534.6,-553.9,-595.2,-584.9,-612.4,-602.8,-614.6
    Government,2250.4,2272,2290.4,2305.7,2300.9,2335.1,2342,2343.7
    Residual,-27.9,-24,-16.8,-20.1,-18.8,-14.7,-11,-8.1
    
    You should display the graph:

    You may include labels, but you do not have to.

Here's xkcd on the simplicity of python:

(This file was last modified on 9 May 2014.)