Study the code examples we discussed in class this week.
Read Proof or Consequences by John Casti (supplementary reading #7).
Using pencil and paper, work through discussion question 2 on page 227 of the textbook. To check your answers, type in the program and try it out in Python.
Write a function called numDaysInMonth(month, year) that takes two input parameters: a month number in the range 1-12 and a year such as 2006. The function should return (not print) the number of days in the indicated month and year. The year is required in order to handle leap years. Hint: use the leap year code we developed in class as a building block. Examples:
>>> numDaysInMonth(10, 2006) 31 >>> numDaysInMonth(2, 2006) 28 >>> numDaysInMonth(2, 2000) 29
A person's body mass index (BMI) is calculated as their weight (in pounds) times 720, divided by the square of their height (in inches). A BMI in the range 19-25, inclusive, is considered healthy. Write a program called bmi() that calculates a person's BMI and prints a message telling them whether they are in the healthy range, or else need to see their doctor. Hint: in Python, conditional tests with multiple operators are permitted, such as 0 < x < 5, in addition to single-operator tests like x >= 3.
Write a program called dice() that simulates repeatedly rolling two dice until both come up with the same number. In addition to printing out the dice values, if the program rolls double ones it should print out "Snake eyes!" If double sixes are rolled, it should print out "Box cars!" Otherwise, it should just print "Doubles!" For example, a sample run of the program might look like the following:
>>> dice() We will simulate rolling two dice until we get doubles... 5 3 1 4 2 6 1 1 Snake eyes! >>>
Write a program called craps() that simulates the casino game of craps, which is played as follows. You start by rolling two six-sided dice and looking at the total. The game then breaks down into the following cases based on that first roll:
Now that we have if/else and while statements in our toolbox, we can spice up our graphical images by making them move around the screen indefinitely. Do exercise 17 on page 230 of the textbook, but instead of using a counted loop (i.e. a for-loop), use a while-loop that loops indefinitely. Recall that the operation object.move(dx, dy) moves a graphical object dx pixels in the horizontal direction and dy pixels in the vertical direction. Call your program bounce().
The game of Rock-Paper-Scissors is played by two opponents, and consists of a series of rounds. On each round, the players simultaneously say either "rock", "paper", or "scissors", and the winner of the round is determined as follows:
Write a program to simulate a game of Rock-Paper-Scissors between you and the computer. Call your program rps(). On each round, the computer should make a random choice and then ask you for your choice, after which it should reveal its choice and report the winner of the round. The game continues until the user enters "quit", "done", or "bye". Your program should behave as shown below (the user's input is in boldface). Note that invalid choices by the user are rejected. For some additional fun, check out the World RPS Society web site for a summary of the rules, info on upcoming tournaments (!) and much, much more.
>>> rps() Welcome to Rock-Paper-Scissors! Rock, Paper, or Scissors? paper You chose paper, I chose scissors Scissors cut paper, so I win! Ha ha! Rock, Paper, or Scissors? rock You chose rock, I chose scissors Rock dulls scissors, so you win. Hmmmph. Rock, Paper, or Scissors? rattlesnake Hey, that's not a valid response! Rock, Paper, or Scissors? scissors We both chose scissors, so nobody wins. Rock, Paper, or Scissors? bye Goodbye! >>>
Simulate the wandering of an intoxicated person in a square street grid. Draw a grid of 30 streets horizontally and 30 streets vertically. Represent the simulated drunkard by a dot, initially blue, placed in the middle of the grid to start. For 100 time steps, have the simulated drunkard randomly pick a direction (east, west, north, south), move one block in the chosen direction, and redraw the dot in red. After the iterations, display the final position as a green dot and show the distance that the drunkard has covered as a blue line connecting the starting and ending points. (One might expect that on average the person might not get anywhere because the moves to different directions cancel each other out in the long run, but in fact it can be shown that the person will eventually move outside of any finite region.) Call your program randomWalk().
Put all of your program definitions into a single file called assign6.py. Print out this file and turn it in to me in class on Wednesday. If you have questions about anything, don't hesitate to ask!