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.