Homework 2
The deadline is Thursday, the 15th of September, 23:59 (Estonian time).
Homework 2.1 Estonian elections
In Estonia local and parliament elections are conducted every 4 years. The mandatory age to vote in local elections is 16 and for parliament, it’s 18. Write a program that asks the user for his/her age and tells them in which elections they can vote. Also, the program has to check the validity of the entered age: the maximum allowed age is 140, a valid age cannot be negative.
Example:
>>> %Run solution.py
Enter your age: 10
You are too young to participate in government elections in Estonia.
>>> %Run solution.py
Enter your age: 17
You can only vote at local elections.
>>> %Run solution.py
Enter your age: -4
Entered age is invalid.
>>> %Run solution.py
Enter your age: 25
You can vote in both local and parliamentary elections.
>>> %Run solution.py
Enter your age: 200
Entered age is invalid.
>>> %Run solution.py
Enter your age: x
Entered age is invalid.
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee/#/ts/620269b86449631d0c385f26/
. It tries to explain the most common problems and give hints.
Homework 2.2 Divisibility
Write a program that asks the user for a number and checks if it can be divided by 3 and/or 5. If the selected number can only be divided by 3 or 5 then print the one that it is divisible with. In case the number is divisible with both 3 and 5 the program should only print that it can be divided by both. If it’s not divisible then inform the user about it. You should also handle non-integer inputs.
Example:
>>> %Run solution.py
Enter a number: 9
Number 9 is divisible by 3
>>> %Run solution.py
Enter a number: 10
Number 10 is divisible by 5
>>> %Run solution.py
Enter a number: 15
Number 15 is divisible by 3 and 5
>>> %Run solution.py
Enter a number: 1
Number 1 cannot be divided by 3 or 5
>>> %Run solution.py
Enter a number: hello world
The inserted value was not a number
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee/#/ts/62035fa56449631d0c38629c/
. It tries to explain the most common problems and give hints.
Go to Moodle and upload your solution into Homework 2.