Python Lesson 8 Exercises: Recursion and Fruitful Functions
In the following exercise, pay attention to your user interface, and be certain to test the programs rigorously. Save each program as pa8-1.py, pa8-2.py and so forth.
Exercise 1:
Create a function that will take two numbers, a and b, and return what percent a is of b. Add this function to the end of your election program (pa5-2.py) so that it will display what percentage of the total vote each candidate received.
Exercise 2:
Create a program that, when given a number, will list all the factors of that number. (A factor is any number that divides into the larger number with no remainder or fractional portion.)
Exercise 3:
Write a math drill program. The program should allow students to pick the type of math questions they want (addition, subtraction, multiplication, etc.) and ask them a series of random questions, keeping score of how many they got right and how many they missed. At the end of the program, the program should tell the user how many questions he was asked and how many he got right.
Exercise 4:
Create a program that will randomly pick a number then play a guessing game with the user. The user should be given a chance to enter a number. The computer should tell the user whether the number is too low or too high, then give a chance to pick another number. This should continue until the user picks the right number.
**Extra Credit:**
Create a roulette game. In the game, a player should start with a given amount of money. Before each "spin of the wheel", the player should be asked how much he wishes to bet and what number he wants to bet on within a range of 0-36. The game should then choose a random number in the range 0-36 and either reward the player if he chose the right number, or subtract from his account if he did not. The player should be allowed to continue playing until he runs out of money.
Restricted access |