Mighty Mary's Smash Hut
Mary owns Mighty Mary's Smash Hut, a boxing school in Rochester Hills. She has a special offer, an intro to face smashing course, at a special price. Even cheaper if you have a Groupon. Write a program to compute the price of an order.
Here's some I/O.
- Mighty Mary's Smash Hut
- ====== ====== ===== ===
- Special offer: Face Smashing 101
- How many people (1 to 4)? 2
- Do you have a groupon (Y/N)? n
- Do you want to rent headgear (Y/N)? y
- Do you want to rent body pads (Y/N)?y
- Price: 600
More:
- Mighty Mary's Smash Hut
- ====== ====== ===== ===
- Special offer: Face Smashing 101
- How many people (1 to 4)? 3
- Do you have a groupon (Y/N)? y
- Do you want to rent headgear (Y/N)? n
- Do you want to rent body pads (Y/N)? y
- Price: 387
- You should rent protective gear!
Even more:
- Mighty Mary's Smash Hut
- ====== ====== ===== ===
- Special offer: Face Smashing 101
- How many people (1 to 4)? some
- Sorry, you must enter a whole number.
- How many people (1 to 4)? 3.65
- Sorry, you must enter a whole number.
- How many people (1 to 4)? 4444
- Sorry, please enter a number from 1 to 4.
- How many people (1 to 4)? 3
- Do you have a groupon (Y/N)? not sure
- Please enter Y or N.
- Do you have a groupon (Y/N)? y
- Do you want to rent headgear (Y/N)? maybe
- Please enter Y or N.
- Do you want to rent headgear (Y/N)? y
- Do you want to rent body pads (Y/N)? perhaps
- Please enter Y or N.
- Do you want to rent body pads (Y/N)? n
- Price: 357
- You should rent protective gear!
The price is computed as follows:
- The base price per person is $99 with a Groupon, or $250 without one.
- Headgear rental costs $20 per person, whether or not there is a Groupon.
- Body pads rental cost $30 per person, whether or not there is a Groupon.
For all input, keep asking until the user enters valid values.
- People count must be an int from 1 to 4.
- Other inputs should be y or n. Strip spaces, and allow upper- and lowercase.
If the user says they don't want headgear and/or body pads (either or both), show a warning after the price.
Write a reusable function to get one Y/N input at a time. Call it three times.
Upload a zip of your project folder. The usual coding standards apply.
Clown clearance
The sleepy town of Fall's End, Montana, has been invaded by evil aliens! They have turned the townsfolk into lava lamps. There are roughly forty aliens, though the exact number is not known.
Poor research led the aliens to disguise themselves as clowns. They thought they would blend in with the locals.
Marcella has discovered felines are the only creatures unaffected by the aliens' transmogrifiers.
Write a program to help her figure out how many leopards, lions, and tigers to rent.
Here's some I/O:
- How many leopards? 4
- How many lions? 4
- How many tigers? 3
- Clowns eaten: 45
- Price: $88400
Here's some more:
- How many leopards? 5
- How many lions? 5
- How many tigers? 3
- Clowns eaten: 51
- Price: $100900
- Warning: you don't have that much.
And even more:
- Clown clearance
- ===== =========
- How many leopards? lots
- Sorry, you must enter a whole number.
- How many leopards? 3.5
- Sorry, you must enter a whole number.
- How many leopards? 3939
- Sorry, please enter a number from 0 to 6.
- How many leopards? 1
- How many lions? -44
- Sorry, please enter a number from 0 to 5.
- How many lions? 1
- How many tigers?
- Sorry, you must enter a whole number.
- How many tigers? 1
- Clowns eaten: 13
- Price: $25300
Validate the three inputs. Keep asking until the user enters valid values. They must be integers, and cannot be less than zero. In addition, leopards cannot be more than six, lions cannot be more than five, and tigers cannot be more than three.
Write a reusable function to get one numeric input at a time. Call it three times.
Compute and output the cost, and the number of clowns eaten. Each leopard costs $4,000, and will eat two clowns. Each lion costs $8,500, and will eat four clowns. Each tiger costs $12,800, and will eat seven clowns.
Marcella has $90,000 in her wallet. If the total cost is more than $90,000, show a warning message along with the output.
Upload a zip of your project folder. The usual coding standards apply.
Aussie rules results
Earlier, you wrote a program to compute a score for an Aussie rules football game. Let's extend that:
- Two teams
- Validation
- Code reuse with functions
Here's some I/O:
- Aussie rules scores
- - - - - - - - - -
- What is the name of team 1? Wombats
- Please enter goals and behinds for Wombats.
- Goals? 3
- Behinds? 5
- What is the name of team 2? Goattos
- Please enter goals and behinds for Goattos.
- Goals? 4
- Behinds? 0
- Goattos (24 points) beat Wombats (23 points)!
- OK, bye!
Remember there are two ways of scoring points:
- Goals, worth six points each.
- Behinds, worth one point each.
The user enters the name of the first team, and the number of goals and behinds they scored. Same for team 2. Output is a message, showing who won and what the scores were. If it's a tie, output "It was a draw."
Here's the main program. Use exactly this code. Your job is to add the functions.
- print('Aussie rules scores')
- print(' - - - - - - - - - ')
- # Input
- # Team 1
- team1_name = get_team_name('What is the name of team 1? ')
- prompt = 'Please enter goals and behinds for ' + team1_name + '.'
- team1_goals, team1_behinds = get_team_results(prompt)
- # Team 2
- team2_name = get_team_name('What is the name of team 2? ')
- prompt = 'Please enter goals and behinds for ' + team2_name + '.'
- team2_goals, team2_behinds = get_team_results(prompt)
- # Processing.
- team1_score = compute_score(team1_goals, team1_behinds)
- team2_score = compute_score(team2_goals, team2_behinds)
- output_message = compute_output_message(team1_name, team1_score, team2_name, team2_score)
- # Output.
- print(output_message)
- print('OK, bye!')
get_team_name
takes a prompt, and keeps asking until the user enters a string. E.g.,
- Aussie rules scores
- - - - - - - - - -
- What is the name of team 1?
- Sorry, you must enter a value.
- What is the name of team 1?
- Sorry, you must enter a value.
- What is the name of team 1? Doopers
- Please enter goals and behinds for Doopers.
- Goals?
You saw code in this module you can use for that.
get_team_results
takes a prompt, and returns two numbers, for goals and behinds. It validates, of course. You can see the error messages here.
- Please enter goals and behinds for Doopers.
- Goals? some
- Sorry, you must enter a whole number.
- Goals? -2
- Sorry, please enter a number between 0 and 20.
- Goals? 333
- Sorry, please enter a number between 0 and 20.
- Goals? 3
- Behinds? a few
- Sorry, you must enter a whole number.
- Behinds? 2332
- Sorry, please enter a number between 0 and 30.
- Behinds? 12
- What is the name of team 2?
Hint: my version of get_team_results
has three lines between the def
and return
:
- def get_team_results(prompt):
- # Three lines here.
- return goals, behinds
You already have a function that gets a valid numeric input. You can reuse it here.
compute_score
takes the number of goals and behinds, and returns the score. For me, the entire function, including the def
and return
, was three lines. I could have done it in two.
My compute_output_message
was eight lines, including the def
and return
. It's just some if
s. The hardest part was putting the scores into the output message. Not that it's difficult. You did things like that early in the course.
This exercise isn't about writing lots of code, but stitching together reusable chunks.
Add comments to each function, in the format we've used. Feel free to copy validation functions from earlier in the book. Upload your solution here, not to Moodle. The usual standards apply.
Phelten party pricing
Write a program to work out the price for catering a party on the planet Phelte. There are four partying species: squils, dephants, smoooshshs, and pleezles. Different numbers of each can be invited to a party. There's a limit to the number that can be invited. For example, smoooshshs are big, so you can only invite a few. Pleezles are tiny, so you can invite many.
The price depends on the amount of food and water needed.
Each species eats the same food: toes. This table shows the maximum number of each species you can invite, and the kilos of toes an individual will eat.
Species | Max | Toes consumed (kilos) |
---|---|---|
Squils | 30 | 0.4 |
Dephants | 50 | 0.6 |
Smoooshshs | 10 | 1.2 |
Pleezles | 1,000 | 0.08 |
A kilo of toes is priced at GC 15.9. (That's galactic credits.)
One liter of water is needed for every kilo of toes. The price of a liter of water depends on the type of water ordered.
- Salt water is GC 0.2 per liter.
- Fresh water is GC 0.7 per liter.
- Distilled water is GC 1.3 per liter.
Your program should ask the user how many of each species will be at the party, and the type of water they want. Validate all input.
- Species numbers must be integers that are from zero to the species max given in the table above.
- Water is one of the three types: S, F, or D. Let users enter upper or lowercase values, and extra spaces if they want.
Output kilos of toes needed, toes price, water price, and the total, as shown below. Round all numbers to at most two decimal places.
Here's some IO:
- Party catering cost
- ===================
- How many squils? 1
- How many dephants? 1
- How many smoooshshs? 1
- How many pleezles? 1
- Water type (S for salt, F for fresh, D for distilled)? s
- - - - - - - - - - - - - - - -
- Toes (kilos): 2.28
- Toes price (GC): 36.25
- Water price (GC): 0.46
- =============================
- Total (GC): 36.71
Here's some more, showing validation:
- Party catering cost
- ===================
- How many squils? some
- Sorry, you must enter a whole number.
- How many squils? 1.4
- Sorry, you must enter a whole number.
- How many squils? -33
- Sorry, please enter a whole number between 0 and 30.
- How many squils? 232
- Sorry, please enter a whole number between 0 and 30.
- How many squils? 10
- How many dephants? 10
- How many smoooshshs? 11
- Sorry, please enter a whole number between 0 and 10.
- How many smoooshshs? 10
- How many pleezles? 100
- Water type (S for salt, F for fresh, D for distilled)? not sure
- Sorry, you must enter S, F, or D.
- Water type (S for salt, F for fresh, D for distilled)? F
- - - - - - - - - - - - - - - -
- Toes (kilos): 30.0
- Toes price (GC): 477.0
- Water price (GC): 21.0
- =============================
- Total (GC): 498.0
The number of zeros don't have to match exactly. 498, 498.0, and 498.00 are all OK.
Other requirements:
- Write just one function that will get the number of a species from the user. Call the function four times.
- Write a function that will get the type of water from the user.
- Write a function that will compute the cost of the water, given kilos of toes and water type.
- You can add other functions if you want.
- Format the output as shown in the IO samples. Include the prompts and the underlining.
- You can add other functions if you want.
Upload your solution here, not to Moodle. The usual programming standards apply.