python programming4
ISTA 130: Homework 4
Due June 10
Write loops to perform the following functions:
1. (2 pt.) Get positive numbers from a user and find and print the smallest number in all of the ones that they enter. Stop asking for numbers after they enter 0 or a negative number
2. (2 pt.) Declare a variable result to be 0. Get a number from the user; if it is even add it to result but if it is odd subtract it from result. Continue this until result is either less than 100 or greater than 100. Then print out result.
The following question uses your functions.py file from lab 4.
3. (4 pt.) Write a Python function named leap year that gets a year from a user and returns True or False based on whether it is a leap year or not.
A leap year is one with 366 days. A year is a leap year if it is divisible by 4 (for example, 1980), except that it is not a leap year if it is divisible by 100 (for example 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). There were no exceptions to the divisible by 4 rule before the introduction of the Gregorian calendar on October 15, 1582 (for example, 1500 was a leap year).
Uncomment the tests in modtester.py and run them. Modify your functions until the tests pass.
1
4. (7 pt.) In a new file named hw4game.py, write a program to play the 21 game.
In this game, there is a pile of 21 stones. The first player takes away 1, 2, or 3 of them. Then the second player takes away 1, 2, or 3 of them. The players take turns removing stones, and the player who removes the last stone loses.
Suggestions to include in your code:
� a loop to let the players take turns removing stones
� a variable to keep track of how many stones there are left
� a variable to keep track of whose turn it is
� if statements to handle input errors (you may want to add these after the rest of the game is working)
Example run:
The 21 game!
The person who picks up the last stone loses.
There are 21 stones left.
Player 1, remove how many stones? (1, 2, or 3)
>>> 3
There are 18 stones left.
Player 2, remove how many stones? (1, 2, or 3)
>>> 2
There are 16 stones left.
...
There is 1 stone left.
Player 2, remove how many stones? (1, 2, or 3)
>>> 2
There are not that many stones left.
Player 2, remove how many stones? (1, 2, or 3)
>>> 1
Player 2 loses!
This doesn’t need to be a really big program; my code to play this game is 21 lines long, not including comments.
Extra credit (2 pt.) for handling grammar and input errors correctly, as in the example above.
Total Points: 15 points possible Extra Credit: 2 points possible
2