************************************************************************************
Homework for Thursday, September 14
- Think of a simple task that requires a series of steps to carry out and write an algorithm for how to do it. There is nothing to hand in. Just be ready to share your algorithm with the class.
- If you have time, also read Chapter 1 pages 1-13 and 20-23. (due next Tues.)
Reminder
- Please bring your smart phone to class today.
************************************************************************************
Homework 1
Due Tuesday, September 19
- Read
- Chapter 1 pages 1-13 and 20-23.
- Chapter 2 pages 31-54
- Neatly by hand complete the following. Put your answers to B. and C. on the back of the worksheet.
- A. Homework 1 Worksheet - Evaluating Arithmetic Expressions
- Figure out and write down the answers. Then usePython to check your work.
- B. Exercise 1.2.2 (pg. 10)
- C. Exercise 2.2.1 (pg. 37)
- Write down the statement(s) used to compute the answer and the answer Python gives you if it differs from your answer.
************************************************************************************
Homework2A
Due Thursday, September 21
- Finish reading the following pages if you have not already done so.
- Chapter 1 pages 1-13 and 20-23.
- Chapter 2 pages 31-54
- Write down the decimal numbers from 0 - 32 and their binary equivalents. Example:
- 0 0000
- 1 0001
- etc.
- Be ready for Quiz 1.
- Don't be late. It will be given during the first few minutes of class.
************************************************************************************
************************************************************************************
Homework2B
Due Tuesday, September 26
- Read Chapter 3 pages 65-75.
- Complete the In-Class Labwe began during class Thursday if you haven't done this yet.
- Save each of the 4 Python exercises in your K: drive folder named Week2Labs, which should be under your Labs folder. Example
K:\17FA\CSC_140_1\Your Name\Labs\Week 2 Labs
- Print and complete the Homework 2 assignment.
- This will be collected and graded.
************************************************************************************
Homework3A
Due Thursday, September 28
- Complete Homework 3A, which was handed out in class.
- For Part I (DaysToWeeks) followthe directions for what to put on your K: drive and what to print to hand in.
- For Part II (turtle graphics) just copy the Labs/Week3 folder to YOUR K: drive Labs folder, openturtleSquares.py in Notepad++, and run itto see how it works. Then modify it as requested to draw a third square. There is nothing to hand in.
- Be ready for Quiz 2 over the material introduced in week 2.
- Remember to be on time. The quiz will be at the beginning of the period.
************************************************************************************
Homework3B
Due Tuesday, October 3
- There is nothing to read this weekend.
- In the K: drive common_area you will find a folder named Homework. That folder contains four Python files:Hw3B-Ex1,Hw3B-Ex2,Hw3B-Ex3, andHw3B-Ex4.
- You can also click the above links to get the files. But then you will receive them as text files. You will need to open them in Notepad++ and save them as Python files before you can use them.
- Follow the directions in each of the files. Once you have completed these you will have printed 4 files. Please put them in order and staple the 4 pages together to hand in.
************************************************************************************
Looking Ahead
The Thursday, October 5th, quiz will be on
- if statements
- Counting loops
- Writing and calling functions, including passing parameters
************************************************************************************
************************************************************************************
Homework 4A
Due Thursday, October 3
- Finish revising Homework 3B Exercises 2 & 3 if you have not handed them in yet.
- Create a K: drive Homework/Week4 folder and put the following homeworkthere.
- Copy theturtleSquares.py program you did as a week 3 lab to this new Week4 Homework folder and modify the programso that each of thesquares has a line width of 3 pixels and is created with a loop that loops 4 times.
- Make square 1red with purple fill.
- Make square 2 blue with green fill.
- Make square 3 green with yellow fill.
- In a single file named Ch3-Exercises.py do the following three page 86-87 problems.
- 3.3.6
- When the "main" code calls drawRectangle itneeds to pass it the name of the turtle and the length and width of the sides. Use 50 for the length, and 30 for the width. The for loop in the function needs to execute twice, drawing a long side (length) and a short side (width) each time.
- 3.3.7
- When the "main" code calls drawPolygon itneeds to pass it the name of the turtle, the side length, and the number of sides. The function will need to determine what angle to turn each time so all the turns add up to 360 degrees. A for loop should be used to draw the sides. Think about how many times it needs to loop. Try running the program with a side length of 40 and 12 sides.
- 3.3.11
- The "main" code will call drawRow, passing it the turtle name.
- Have the drawRow function call yourdrawRectanglefunction, passing it the turtle name, 12 for the length and 12 for the width,to draw each square.
- The drawRow function should loop 4 times, each time drawing a red filled square followed to its immediate right by a black filled square.
- Be ready for this week's quiz on
- if statements
- Counting loops
- Writing and calling functions, including passing parameters
************************************************************************************
************************************************************************************
Homework 4B
Due Tuesday, October 10
- Finish reading Chapter 3 Sections 3.1 - 3.5.
- Do not read Section 3.6, as it is apt to give you the wrong idea about how parameter passing works.
- Complete the following Ch. 3 and Ch. 4 exercises and put the 3 files youcreate in yourK: drive Homework/Week4 folder. Hand in just the things requested below. In all 3 programs make main() a function and start the program running by calling it.
- 3.3.13 (pg. 87) - Checkerboard.py
- Pass the checkerboard function 30 as the length of each side.
- Print the program to hand in. On the bottom or back draw a neat structure chart that illustrates which functions call which other functions. The box named main will be at the top.
- 3.5.8 (pg. 102) - computeBMI.py
- Prompt the user to enter their height in inches and weight in pounds. Then call your computeBMI function to compute and return the user's bmi. In addition to printing out this information along with a good label, print a message telling the user if they are underweight (bmi < 18.5), overweight (bmi > 25.0), or an optimal weight (bmi 18.5 - 25.0). Run the program 3 times, once for a bmi that falls into each of the 3 categories, and paste the output as a comment at the bottom of the program. Print this program to hand in.
- 4.1.2, 4.1.10, and 4.1.11 (pgs. 129-131) all in the same program - Triangles.py
- Create 3 functions, one to print each of the triangles. These functions do not return anything.
- Have main() print your name on the screen and then call each of the functions.
- After each of these 4 lines in main() add the following line to separate the triangles on the screen: print('\n\n')
- You must use a loop to generate each triangle. Use the loop control variable's value each time through the loop to figure out how many stars to print on the current line, and in the case of 4.1.11 how many blank spaces to print before printing the required number of stars.
- Note that this program does not use turtles. The triangles should just appear one after another on the "black" screen.
- Hint: You will likely find the string repetition operator (*) useful. See the bottom of page 48.
- Want more of a challenge?
- If so, do Exercise4.1.12 by adding a diamond() function to your Triangles program and having main() call it too.
************************************************************************************
************************************************************************************
Reminder
Prof. Walters will be holding extra office hours
Wed., October 11, from 2:00 - 4:00 p.m.
to work with anyone wanting help to prepare for our Thursday midterm.
************************************************************************************
Homework for Thursday, Oct. 12
Get ready for the midterm exam.
It is a 1-hour, open book, closed notes, exam.
It will begin at about 2:45.
You may leave once you are done.
Anyone needing extra time may work up until 4:30 if theirschedule permits.
************************************************************************************
Homework for Tuesday, Oct. 17
- Read Chapter 4 Sections 4.1 and 4.3, which we will be covering in class today.
- Program 1 (Version 1), handed out in class on Oct. 10, is due today.
************************************************************************************
Homework for Thursday, Oct. 19
- Click here to get Homework 6A, due Thursday.
- It consists ofthree exercises thatstart with already written programs, so it should be quick to complete. Most people will, I think, be able to do all 3 in just 1 – 2 hours.
- The two new files the exercises use are now in the K: drive common_area Homework/Week6 folder. The bankAcct and withdrawals files we examined at the end of class Tuesday should already be in your In-Class Examples/Week6 folder.
- You can also get a text version of any of these by clicking on the links below. If you do this you will need toopen them in Notepad++, and then save them as .py files before you can work with them.
- bankAcctwithdrawalsscopeformattingOutput3
- Be ready for this week's quiz on passing arguments to functions, scope of variables, and working with random numbers.
************************************************************************************
Homework for Tuesday, Oct. 24
- Complete Homework 6B - Modeling a Stock Market Account
- Complete Version 2 of the Dice Program
Click here for the weather6 program (in text form) that shows how to use an integer
variable as an index to add to the correct accumulator in a list of accumulators.
************************************************************************************
Homework for Thursday, Oct. 26
- Read Chapter 5 Sections 5.4 and 5.5
- Complete Homework 7A - Ex. 1 - Using Random Numbers
- The file file it uses is in the K: drive common_area\Homework\Week7 folder.
- Run the program and paste the results where requested.
- Then make sure you understand WHY it displayed what it did in each case.
- Bring the print out to class Thurs. so we can go over it. It will not be collected.
- Neatly complete the following Homework 7A exercises on paper.
- These will be collected and graded.
- 5.1.10 (pg. 199) Add d) 7, 7, 0.
- Why does this give wrong results? Rewrite the code so that it works correctly and is more efficient.
- 5.1.13 (pg. 199) 5.4.6 (pg. 221) 5.4.11 (pg. 221) 5.4.16 (pg. 222)
- Your revised Homework 6B - Modeling a Stock Market Account (if needed) is due.
- Be ready for Quiz 5.
- It covers random numbers, growth & decay models, and formatting output.
***********************************************************************************
Reminders
Professor Walters is out of town this weekend and
will not be holding office hours on Monday, Oct. 30.
Instead, office hours will be held on Wednesday, Nov. 1,from 2:00 - 4:00
The first 50 minutes of class on Tuesday, Oct. 31, will belab time for partners
to work togetheronProgram 2. The instructional part of class will begin at 2:50.
************************************************************************************
************************************************************************************
Homework 7B ~ Due Tuesday, Oct. 31
- Read
- Chapter 5 Sections 5.4 and 5.5 if you have not yet done so.
- Chapter 5 Section 6.1
- Look at the Booleans.py file that is in your In-Class Examples/Week 7 folder. This is the program we started looking at in class Thursday. Figure out what eachprint statement will print. Then run the program to see if you were correct.
- Look at the numberGuessingProgram.py that is in the same folder. It is the binary search program we looked at in class. Make sure you understand how it works.
- Look at the Ex-5-4-13.py program in the same folder. If you have not copied it to your own K: drive yet,find it in the common_area/In-Class Examples/Week 7folder and copy it to your folder now.It is a solution toexercise 5.4.13 on page 222 of your book that uses alist ofdata values to test the program without having to input the values.Run the program to see how it works. Then study the code to see how it does it. You need to use this idea in completing the following two exercises.
- Two exercises to complete on the computer and hand in. Place each one in your K: drive Homework/Week 7 folder, run it,and paste the output as a comment at the bottom.
- Ex. 5.4.16 (pg. 222) - This is the same leap(year) function you wrote on paper for Hw 7A, but now youneed to write a program that includes this function as well as amain function that calls it. Have main loop to call the function 6 times and test the Boolean value it returns to print eachresult like this:
2012 is a leap year.
2013 is not a leap year.
The year that you pass to the leap function must be retrieved from the following
list you create in main:years = [2012, 2013, 1996, 2000, 1985, 1900].
- Write a program containing the function RepeatedChar(aString,ch) that is passed a string and a character to search for in it. The function should return True if the character appears more than once in the string and False otherwise. Have themain function create the following string:
Greeting ='Happy Halloween, Ghosts!'
andthe following list :
searchChars = ['y', 'p', 'b', 's', 'h']
Make mainloop once for each element in searchChars. Each time it loops, have it callRepeatedChar and pass it Greetingand the next character from the list. It should then test the Boolean value it gets back to print a result like this:
Happy Halloween, Ghosts!contains at most one y.
Happy Halloween, Ghosts!contains two or more p's.
Remember to count both upper case and lower case instances of any letter.
- Work with your partner to startdesigning and implementing Program 2 - Version 1.
************************************************************************************
************************************************************************************
HomeworkDue Thursday, Nov. 2
- Read
- Chapter 6 Section 6.1 if you have not yet done so.
- Look at the Ex-5-4-13.py program in yourIn-Class Examples/Week7 folder if you have not yet done so.It is a solution toexercise 5.4.13 on page 222 of your book that uses alist ofdata values to test the program without having to input the values. You need to use this ideain completing the Homework 7Bexercises.
- Complete Homework 7B. This consists oftwo exercises to complete on the computer and hand in. These were originally due on Tuesday, Oct. 1, but are now due on Thursday, November 2. Place each one in your K: drive Homework/Week7 folder, run it,and paste the output as a comment at the bottom.
- Ex. 5.4.16 (pg. 222) - This is the same leap(year) function you wrote on paper for Hw 7A, but now youneed to write a program that includes this function as well as amain function that calls it. Have main loop to call the function 6 times and test the Boolean value it returns to print eachresult like this:
2012 is a leap year.
2013 is not a leap year.
The year that you pass to the leap function must be retrieved from the following
list you create in main:years = [2012, 2013, 1996, 2000, 1985, 1900].
- Write a program containing the function RepeatedChar(aString,ch) that is passed a string and a character to search for in it. The function should return True if the character appears more than once in the string and False otherwise. Have themain function create the following string:
Greeting ='Happy Halloween, Ghosts!'
andthe following list :
searchChars = ['y', 'p', 'b', 's', 'h']
Make mainloop once for each element in searchChars. Each time it loops, have it call RepeatedChar and pass it Greeting and the next character from the list. It should then test the Boolean value it gets back to print a result like this:
Happy Halloween, Ghosts!contains at most one y.
Happy Halloween, Ghosts!contains two or more p's.
Remember to count both upper case and lower case instances of any letter.
Hint: You may find thelower() string function (pg. 705) and the len()function (pg. 353) that returns the length oflists and strings useful.
- Finish Program 2 - Version 1.
- Be ready for Quiz 6 on lists, Boolean operators, Boolean variables, functions that return a Boolean value, and the in and not in operators.
***************************************************************************************
****************************************************************************************
Homework8 Due Tuesday,Nov. 7
- Turn the soccer goals program pseudocode I covered in class into a Python program.
- Retrieve the team names from the following list:
Team # 0 1 2
teamName = ['Warriors', 'Panthers', 'Demons']
- Retrieve the goal data from the following list:
Data for Game # 0 1 2 3
soccerData = [ [ [2,0],[2,1],[3,0],[1,0]], # Team 0
[[2,1],[3,2],[1,2],[2,4]], # Team 1
[[2,4],[2,1],[0,2],[2,6]]] # Team 2
- Notice that The main list contains 3 elements, each of which is a team list.
- Each team list contains 4 elements, each of which is a game list.
- Each game list contains 2 numbers. The first is goalsUs and the second is goalsThem.
****************************************************************************************
Looking Ahead
- Start working with your partner onProgram 2 - Version 2 (due Thursday, Nov. 9).
- The sample output I handed out is for a program that allowed up to 12 cars to arrive during each period.Yours should only allow a maximum of 10 cars to arrive.
- This week's Thursday Quiz will be over the min() and max() functions, character and string functions we have covered, returning Boolean values from functions,nested loops, and lists whose members are other lists.
************************************************************************************
Homework Due Thursday,Nov. 9
- There is no new homework for Thursday. Just do the following.
- FinishProgram 2 - Version 2.
- Placethe finished program, with the 2 required run outputs pasted as a comment at the end,in the Programs folder of both partner's K: drives.
- Print just one copy of the program to hand in. Staple the pages together with a fresh copy of the Cover Sheet on the top that has both team members' names on it.
- Be ready for this week'sQuiz. It will include the min() and max() functions, character and string functions we have covered, returning Boolean values from functions,nested loops, and lists whose members are other lists.
************************************************************************************
************************************************************************************
Q & A
Q:When I try to read from the data file I get errors. What I am doing wrong?
A: When you brought this to my attention, Idiscovered that using code such as any of the following does not work for what we are trying to do, although they work for other uses.
1.name = inputFile.read()
2. for line in range(4):
goalsUs = inputFile.read()
goalsThem = inputFile.read()
3.for line in inputFile:
goalsUs = line
goalsThem = line
These do not work because it turns out that theread command ignores the '\n' and reads the ENTIRE file when it sees the very first inputFile.read() command.
Andfor line in inputFile: automatically loops for every line in the file.
To solve the problem, change all file read commands in your program to
inputFile.readline()and use the following code inside your outerloop that iterates once for each team: