2.11. Additional Challenges#
Code challenge: Days Until Christmas
It’s December! That means it’s almost Christmas.
Write a program that asks the user for the day of the month. Your program should either say how many sleeps until Christmas. Note that on the 24th there is only one sleep until Christmas so your program should not say 1 sleeps until Christmas, because it’s grammatically incorrect. If it’s the 25th of December your program should say Merry Christmas!.
You program will only be given values 1-25 inclusive.
Example 1
Today's date: 10
15 sleeps until Christmas
Example 2
Today's date: 24
1 sleep until Christmas
Example 3
Today's date: 25
Merry Christmas!
Solution
Solution is locked
Code challenge: pH Scale
The following algorithm is used to interpret a pH scale.
IF ph == 7 THEN
Display 'neutral'
ELIF ph > 4.5 and ph < 7 THEN
Display 'acidic'
ELIF ph < 4.5 THEN
Display 'very acidic'
ELIF ph > 7 and ph < 10.5 THEN
Display 'alkaline'
ELSE
Display 'very alkaline'
ENDIF
Write the Python code that corresponds to the given pseudocode.
Solution
Solution is locked
Code challenge: Algorithm Implementation
Code challenge: Simple Calculator
Write a program to act as a simple calculator. The program will ask the user for two numbers, x and y (these can be floats). It will then ask the user for an operation and return the corresponding calculation.
Operation |
Calculation |
---|---|
add |
\(x + y\) |
subtract |
\(x - y\) |
multiply |
\(x \times y\) |
divide |
\(x/y\) |
If the user gives an operation that is not on the list your program should output Error! Unknown operation.
The program will then print the result to two decimal places.
Example 1
x: 2
y: 4
Operation: add
6.00
Example 2
x: 9
y: 2
Operation: divide
4.50
Example 3
x: 6
y: 2
Operation: power
Error! Unknown operation.
Solution
Solution is locked
Code challenge: HSC Bands
Write a program that asks for the user’s mark, and then states their corresponding HSC band.
Mark |
Grade |
---|---|
0 - 49 |
Band 1 |
50 - 59 |
Band 2 |
60 - 69 |
Band 3 |
70 - 79 |
Band 4 |
80 - 89 |
Band 5 |
90 - 100 |
Band 6 |
Here are some examples of how your code should run.
Example 1
Enter your mark: 63
Band 3
Example 2
Enter your mark: 82
Band 5
Example 3
Enter your mark: 24
Band 1
Solution
Solution is locked
Code challenge: Tennis Calls
Write a program that converts tennis points to the umpire’s call. It’s a little confusing but here are how the first four points are scored.
0 |
Love |
---|---|
1 |
15 |
2 |
30 |
3 |
40 |
Examples:
Score: 2-0 \(\rightarrow\) 30-Love.
Score: 2-3 \(\rightarrow\) 30-40.
Once is on 4 points or more it gets even more complicated. Here are the call rules.
Score |
Call |
---|---|
Player 1 and Player 2 have the same score |
Deuce |
Player 1 score > Player 2 score |
Advantage player 1 |
Player 1 score < Player 2 score |
Advantage player 2 |
Examples:
Score: 5-5 \(\rightarrow\) Deuce.
Score: 4-5 \(\rightarrow\) Advantage player 2.
To win, a player has to have at least 3 points, and then has to have 2 points more than their opponent.
Score: 3-1 \(\rightarrow\) Game. Player 1 wins.
Score: 4-2 \(\rightarrow\) Game. Player 1 wins.
Score 5-7 \(\rightarrow\) Game. Player 2 wins.
Write a program that asks for the tennis scores of two plays and outputs the umpire’s call.
Example 1
Player 1: 1
Player 2: 1
15-15
Example 2
Player 1: 3
Player 2: 2
40-30
Example 3
Player 1: 4
Player 2: 4
Deuce.
Example 4
Player 1: 5
Player 2: 3
Game. Player 1 wins.
Example 5
Player 1: 7
Player 2: 6
Advantage player 1.
Solution
Solution is locked