Homework 2 - due Wednesday, 9/23 before class
Note: For all programming exercises you should - like the last time - write down a description of the algorithm that you are going to implement before you start doing anything with the computer. And like the last time, I want you to hand in both: your algorithm description and your Python implementation. See here for more info on what I am looking for in the algorithm description as well as the Python code.
1) Working overtime
(You will be able to solve this after Monday's class. If you want to attempt it after Friday's read the chapter about if-statements, so that you know about "else" in if-statements.)
Many companies pay time-and-a-half for any hours worked above 40 in a given week. Write a program which asks the user to input the number of hours worked and the hourly rate and which then calculates the total wages for the week.
2) Leap years
(You will be able to solve this after Monday's class. If you want to attempt it after Friday's read the chapter about if-statements, so that you know about "else" in if-statements.)
A year is a leap year if it is divisible by 4, unless it is a century year that is not divisible by 400. (1800 and 1900 are not leap years while 1600 and 2000 are.) Write a program that calculates whether a year (inputted by the user) is a leap year.
3) Bouncing objects
(You should be able to solve this problem after Friday's class.)
-
Assume that you have a simple python program using the pygame library that draws a circle onto the screen. Given a variable with the name my_win which is referring to the pygame window object has been definited, where would the command
pygame.draw.circle(my_win,pygame.color.Color('navy'),(0,0),50)
place the circle on the screen? (Explain in detail how the circle is located with respect to the edges - or draw a sketch illustrating the position, if you find that easier.) -
Download, run, and look at the following two programs: bouncing_ball.py and bouncing_ball2.py.
Compare their behavior. What is different? Explain how that difference is achieved by the code.
-
Starting from bouncing_ball.py, change the code so that the ball bounces off the top and the bottom edges of the pygame window, but not of the left and right edge. Instead, when it disappears over the left or right edge, it should reenter the window on the opposite side.
Submit everything on Blackboard.