Sheet 2
Sheet 2
2) Write a function named coinToss that simulates the tossing of a coin. When you call
the function, it should generate a random number in the range of 1 through 2. If the
random number is 1, the function should display "heads." If the random number is 2,
the function should display "tails." Demonstrate the function in a program that asks the
user how many times the coin should be tossed, and then simulates the tossing of the
coin that number of times.
4) (Parking Charges) A parking garage charges a $2.00 minimum fee to park for up to
three hours. The garage charges an additional $0.50 per hour for each hour or part
thereof in excess of three hours. The maximum charge for any given 24-hour period is
$10.00. Assume that no car parks for longer than 24 hours at a time. Write a program
that calculates and prints the parking charges for each of three customers who parked
their cars in this garage yesterday. You should enter the hours parked for each customer.
Your program should print the results in a neat tabular format and should calculate and
print the total of yesterday’s receipts. The program should use the function
calculateCharges to determine the charge for each customer. Your outputs should
appear in the following format:
The player then types a first guess. The program responds with one of the following:
1. Excellent! You guessed the number!
Would you like to play again (y or n)?
2. Too low. Try again.
3. Too high. Try again.
If the player’s guess is incorrect, your program should loop until the player finally gets
the number right. Your program should keep telling the player Too high or Too low to
help the player “zero in” on the correct answer.
7) Write a recursive function power( base, exponent ) that, when invoked, returns
base exponent
For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater
than or equal to 1.