During session 2
Exercises
1. Grading system
The following subtasks make up a large program. You have to go through the subtasks in the order given below to develop the final version of the program.
1. Write a program that asks the user to enter a score (points obtained in some course). If the score is larger than 50, the program prints out a message saying that the user has passed the course, otherwise the course has been failed.
2. Modify the program so that it also asks the user about the assessment type. Two types are possible: non-differentiated (pass/fail) or differentiated (grades). In case of non-differentiated, the program has to output the same result as in the previous subtask. In case of differentiated, the program has to output the grade related to the score:
Score | Grade |
---|---|
>= 91 | A |
>= 81 | B |
>= 71 | C |
>= 61 | D |
>= 51 | E |
< 51 | F |
3. Modify the program so that it does not stop working if the user enters an inappropriate input (use try-except). Ideally, the user has to enter a numeric value between 0 and 100. If the input is something else, an informative message is displayed.
4. Modify the program so that whatever the user inputs for the course type, the program does not crash (use if-elif-else). If the input is nonsense, a corresponding message is displayed.
Make sure that the program works for all kinds of inputs. Some examples are shown below.
TEST 1:
Enter score: 95 Enter course type (differentiated (d)/non-differentiated (n)): n PASSED
TEST 2:
Enter score: -15 Enter course type (differentiated (d)/non-differentiated (n)): d Score must be in the range 0-100.
TEST 3:
Enter score: 2000 Enter course type (differentiated (d)/non-differentiated (n)): n The score must be in the range of 0 to 100.
TEST 4:
Enter score: 65 Enter course type (differentiated (d)/non-differentiated (n)): d D
TEST 5:
Enter score: 50 Enter course type (differentiated (d)/non-differentiated (n)): d F
TEST 6:
Enter score: 50 Enter course type (differentiated (d)/non-differentiated (n)): don’t know The course type is not recognised.
2. What is the name of the month?
Write a program that asks the user to enter the number of a month and then prints the name of the month. Make sure when an inappropriate input is entered, the program gives some feedback about it (e.g., There are only 12 months in a year.). Test the program with different inputs and make sure it works for them. For example: -100, 0, 1, 2, 5, 9, 10, 12, 13, 100 etc.
Here are some examples of the program output.
Enter the number of month: 2 February
Enter the number of a month: 0 The number of a month must be in the range [1-12]
Enter the number of month: 13 There are only 12 months in a year.
Enter the number of month: one Please enter a number
3. Number of days in month
If you have a lot of if
-statements in your homework solution, rewrite your program using or
operators to have less if
-statements.
If you have not solved the leap year task yet, do it now.
If your solution to the leap year task does not contain any checks for the inputs, modify your program.
4*. Students
Write a program to solve the following problem.
At the beginning of a school year, the program manager has to split the students between the practical sessions. The manager has to divide the students between the classes as equally as possible. Write a program that asks the manager for the number of available class rooms and for the number of students. The program should then output the number of students in each class room.
Test the program with the following inputs:
number of students: 100, number of rooms: 20 number of students: 105, number of rooms: 10 number of students: 1050, number of rooms: 45
Add a comment: why have these values been chosen?
Some examples of the program output are shown below:
Enter number of students: 103 Enter number of rooms: 15 In 13 classes there are 7 students In 2 classes there are 6 students
Enter number of students: 80 Enter number of rooms: 4 In 4 classes there are 20 students
Hints:
- Calculate how many students would be equally divided between the rooms (for example, if there are 11 students and 3 rooms, then each room can equally fit 3 students).
- Calculate how many rooms have to take an extra student in addition to the previously calculated number of students (for example, if there are 11 students and 3 rooms, 2 rooms have to take an extra student).
Remember:
- When you apply the truncated division operator //, the answer is an integer (a whole number without the decimal part); e.g., 7 // 2 = 3.
- The modulus operator (%) returns the remainder of the division. For example, 7 % 2 = 1 because 2 fits into 7 only 3 times (2 + 2 + 2), and the remainder is 1.
You can use both of these operators in your solution.
One more hint (an example with 103 students and 15 rooms):
- Divide the number of students by the number of rooms: 103 // 15, the answer is 6. Hence, at least 6 students have to be in each room.
- Find the remainder of the same division. The remainder of 103 % 15 is 13. Hence, there are 6 + 1 = 7 students in 13 rooms.
- Find the number of the other rooms: number of rooms - previously found number of rooms, 15 - 13 is 2. Hence, there are 6 students in 2 rooms .