Before session 2
Conditional execution
Watch the video:
Slides in English and in Russian
Textbook in English
Test
Go to Moodle and take the second test about the video lecture. You can get up to 2 points for the test. You are not limited in time while taking the test. You can spend as much time as you need. You can answer the questions several times. You can take the test several times. The score of the last attempt is credited.
Exercises
This week we are into if-else and try-except. In real world, the programs are developed by mixing these constructions (and others that we have not learned yet). For example, you can write a program that asks an input, checks if the input value is valid and then depending on the input chooses what to output.
Example 1
age_as_string = raw_input("How old are you? ") try: age = int(age_as_string) except: age = -1 if age < 1: print "This is not a valid age" elif age < 18: print "In Estonia, you are considered as a minor" elif age < 100: print "You are an adult" else: print "Wow, you need a lot of birthday candles!"
Example 2
try: x = int(raw_input('Enter first number: ')) y = int(raw_input('Enter second number: ')) if x == y: print 'Numbers are equal' else: if x < y: print x, 'is less than', y else: print x, 'is greater than', y except: print 'Please enter a number'
Exercise 1. Students
Write a program to solve the following problem. At the beginning of the school year, the program manager has to split the students between the practical sessions. The manager wishes to divide the students between the classes as equally as possible. Write a program that asks the manager for the number of class rooms and for the number of students. The program should then output the number of students in each class room.
Test your 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 were these inputs chosen?
Some examples of the program output are shown below:
Enter number of students: 103 Enter number of rooms: 15 You will need to place 7 students in 13 classes. You will need to place 2 students in 6 classes.
Enter number of students: 80 Enter number of rooms: 4 You will need to place 20 students in 4 classes.
Hints:
- Calculate how many students would be equally divided between the rooms (for example, if you have 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 you have 11 students and 3 rooms, 2 rooms have to take an extra student).
Remember:
- When you apply the division operator (/) to integers, the answer is also an integer (a whole number without the decimal part). For example, 7 / 2 = 3.
- The modulus operator (%) returns the remainder of the 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, in 13 rooms there are 6 + 1 = 7 students.
- Find the number of other rooms: number of rooms - previously found number of rooms, 15 - 13 is 2. Hence, in 2 rooms there are 6 students.
Exercise 2. Number of days in months
Write a program that asks the user to enter the number of the month. The program outputs the number of days in that month. Also make sure if the user inputs anything ridiculous, the program gives some feedback about it. Use try-except.
If you can (and it is not very difficult at all) also try to check against the leap year. In that case, you also have to ask the user for the year. If you can’t do it, don’t worry, do it without the check against the leap year, but we strongly encourage you to try!
Test your program with different inputs and make sure it works for them. For example: -100, 0, 1, 2, 5, 9, 10, 12, 13, 100, blabla
Here are some examples of the program output.
Enter a number of the month: blabla Please enter a number
Enter a number of the month: 0 The number of the month must be in the range 1-12
Enter a number of the month: 12 That month has 31 days in it.
Enter a number of the month: 4 That month has 30 days in it.
In case of number 2 (February), the program might also check against the leap year, but this is optional:
Enter a number of the month: 2 Enter a year: 2015 That month has 28 days in it (a common year).
Enter a number of the month: 2 Enter a year: 2000 That month has 29 days in it (a leap year).
Enter a number of the month: 2 Enter a year: 2100 That month has 28 days in it (common year).
Enter a number of the month: 2 Enter a year: 2016 That month has 29 days in it (a leap year).
Go to Moodle and put your solutions into Homework 2.