Homework for Week 2
After this week you can
- Create programs that perform simple arithmetic calculations with variables
- Get input from the user and write the output on the screen
- Convert values between different types
- Use Python documentation to find information about Python commands
Variables, expressions, and statements
The computer stores data in variables. A variable is a container that holds a value; the computer can put a value into the container and retrieve a value from it. The variables can be used as building blocks to form expressions, the most common of these are arithmetic expressions and logical expressions. When evaluating an expression, the computer retrieves the values of variables appearing there and performs the calculations with the values. A statement is a complete Python command which instructs the computer to do a particular unit of work: assign the value of an expression to a variable, print the value to the screen, get user input, etc.
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.
Examples
Rectangle
Write a program that gets side lengths of a rectangle from the user and prints the area and perimeter of the rectangle.
a = float(input("Enter side length: a = ")) b = float(input("Enter side length: b = ")) S = a * b print("Area of rectangle is", S) P = 2 * (a + b) print("Perimeter of rectangle is", P)
The program gets the values of a and b from the user and converts them to floating-point numbers to be able to perform arithmetic operations with them. Then it computes the area, assigns the result to a third variable, and prints out the value of that variable. The same is repeated with the perimeter.
Fruits
Write a program that prompts the user for a fruit name and prints a message which mentions that name in the plural.
fruit = input("What is your favorite fruit? ") print("Great, " + fruit + "s are my favorite too!")
Note that here we concatenate strings with the operator + since this allows to put strings immediately one after another, without additional characters in between.
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 a 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 + 2. Round the answer to a nearest intger (you can use round for that, e.g., round(2.5) returns 3).
Example 1
Enter your name: Marina
Enter your foot size (cm): 23.5
Dear Marina, your shoe size is 37.
Example 2
Enter your name: Robert
Enter your foot length (cm): 26.5
Dear Robert, your shoe size is 42.
3. Biscuit cake
When making a biscuit cake, the biscuits are laid on a rectangular tray in several layers, each layer consisting of the same number of biscuits. Write a program that prompts the user for the number of biscuits along the width of the tray, the number of biscuits along the length of the tray, and the number of layers, and then prompts for the number of biscuits in one packet. The program outputs the number of packets that must be bought to make the cake of the specified size.
Example 1
Enter width of the cake: 4
Enter length of the cake: 5
Enter number of layers: 6
Enter number of biscuits in one packet: 20
You have to buy 6 packets.
Example 2
Enter width of the cake: 4
Enter length of the cake: 4
Enter number of layers: 11
Enter number of biscuits in one packet: 25
You have to buy 8 packets.
All inputs are integers, as well as the answer. It is not possible to buy half a packet of biscuits.
Test your program! Select at least one set of data, where all packets will be used up completely, and at least one set of data, where some biscuits from the last packet will be left over.
4. Username generation
Write a program that:
- Asks for person's first name
- Asks for person's last name
- Prints the username constructed by writing first name and last 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 the case is mixed, but the program should always print the username in all lowercase.
Example
Enter first name: kALle
Enter last name: KalDUR
kalle.kaldur
Hint
>>> "kaSpaR".lower() kaspar
To join strings, use +.
Additional exercise
In this task, it is acceptable when accented letters remain unchanged (for example, the user enters "Ü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 tasks 2, 3, 4 to the autotester in Moodle, under Homework for Week 2.
NB! Use home2.py, home3.py, home4.py as the names of your files so that the autotester can find them.
Advice
Start early. For example, flight training students are instructed to take off at the beginning of the runway because that part of the runway you have left behind will be of no more use to you.