Homework for Week 2
Variables, expressions and statements
Watch the following videos on variables, expressions and statements:
Slides in English
Textbook in English
Quiz
Go to Moodle and solve the second quiz on video lecture materials.
- Quiz can be solved several times.
- Best attempt counts.
Exercises
1. Python documentation
Study the Python documentation about the math module (http://docs.python.org/3/library/math.html). Find the meanings of floor and ceil commands – you may need them when solving the following exercises. Also review the documentation about string methods (http://docs.python.org/3/library/stdtypes.html#string-methods).
Square brackets in the documentation mean that it is not necessary to give value to that parameter when calling the function, because the parameter has default value. For example, if the description of the method is str.center(width[, fillchar]), then this means that it can be used either with one argument like client_name.center(80) or with two arguments like client_name.center(80, '~').
2. Shoe size
Write a program that prompts the user for his/her name and actual foot size in cm. Then the program computes and outputs the correct shoe size, using the formula: shoe size = 1.5 * (foot length + 1.5). Round the answer (you can use round for that, e.g. round(2.5) returns 3).
Enter your name: Marina Enter your foot size (cm): 23.5 Dear Marina, your shoe size is 38.
3. Power line
When building a power line, poles are erected with equal distances which doesn't exceed a certain maximum distance. The line starts and ends with a pole. Write a program that prompts the user for the length of power line (integer number of meters) and the maximum distance between each two adjacent poles (integer number of meters). The program outputs the minimal number of poles that is necessary to build the power line.
Test your program!
- Choose at least one set of values where all adjacent poles lie at the maximum distance. (For example, length of line 400 m, maximum distance between poles 40 m.)
- Choose at least one set of values where adjacent poles are closer to each other than the maximum distance.
- Choose at least one set of values where the total length of power line is less than maximum distance between adjacent poles.
4. Username generation
Write a program that:
- Asks for person's first name
- Asks for person's second name
- Prints the username that is constructed by writing first name and second name after each other, with all letters in lowercase and both names separated by a dot.
The user may enter the name where all letters are lowercase, all letters are uppercase, or case is mixed but the program should always print the user name in all lowercase.
Example
>>> %Run nimi.py Enter first name: kALle Enter second name: KalDUR kalle.kaldur >>>
Hint
>>> "kaSpaR".lower() kaspar
To join strings, use +.
Additional exercise
In this problem it is acceptable when accented letters remain unchanged (for example, the user enteres „ÜLLE“ and the program changes it to „Ülle“). But if this problem is too easy for you, then try to write a version that changes accented letters to corresponding letters without accent marks, for example, changes „pÕÕsas“ to „poosas“.
Submit your solutions
Submit your solutions of problems 2, 3, 4 to the autograder in Moodle, under Homework for Session 2.
NB! Use home2.py, home3.py, home4.py as the names of your files, so that autograder can find them.