Homework for Week 1
After this week you can
- Use Thonny to write and run programs
- Create programs that perform simple arithmetic calculations with variables
- Convert values between different types
- Get input from the user and write the output on the screen
- Find and correct errors in your programs
Videos
In the role of lectures, we will use the free lecture videos by Chuck Severance from the University of Michigan.
- The videos have manually created subtitles, if you need them.
- Presentations used in the videos are linked under the videos.
- Full text on the topic can be found in the textbook (link under the videos).
The videos below are an introduction to programming. They tell why one might want to learn to program and what are the basic issues when learning how to program.
Slides in English
Textbook in English
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
Thonny
Thonny is an IDE for Python. In this course, we'll use Thonny as our main programming editor. Please install it, so you can write programs and do homeworks on your own computer.
Thonny comes with Python built in - there is no need to install Python separately. Only Thonny installer is needed to start learning to program.
Please download and install Thonny from
If you have problems with installation, please look at:
Please download and install Thonny from
If you have problems with installation, please look at:
Quizzes
Each week we will have Moodle quizzes on lecture material.
- Each quiz is worth up to 0.5 points.
- The quiz can be answered several times before the deadline, the best attempt counts.
Go to Moodle and solve the quiz for Week 1.
Examples
Here are some example programs you can study when doing the homework exercises. They illustrate the practical aspects of the topic.
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
Each week, there will also be programming exercises.
- Each set of programming exercises is worth up to 0.5 points.
- Each task has an autotester in Moodle, and you can submit your work serval times before the deadline.
- While you are strongly encouraged to do so, there is no obligation to solve all tasks correctly. What counts is the effort you make when trying to solve them.
1. Apples
Write a program that solves the following problem.
Use the following program as a basis and write the correct arithmetic expression after the equal sign in the line 'total_apples ='.
x = int(input("How many apples does Anu have? ")) y = int(input("How many more apples does Brita have? ")) total_apples = print("The children have a total of", total_apples, "apples.")
2. 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".
3. 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 length (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.
Submit your solutions
Submit your solutions to exercises 1, 2 and 3 above to the autotester in Moodle.
NB! Name the files as home1.py, home2.py, and home3.py so that autotester can find them.
How to do the homework
Watch the videos or read the materials before attempting the programming tasks. The videos and materials give an overview of the topic and point out the most important things.
Then solve the quiz, which rehearses the building blocks of the topic, both practical and theoretical. Review the videos if needed. You can solve the quiz many times; try to get as many correct answers as you can.
Finally, solve the programming tasks. It is recommended to test and debug your program locally until you have eliminated all mistakes yourself, and only then submit it to the autotester. Don't worry if you are not able to solve a task on the first attempt. Try again tomorrow, be persistent. Homework is a chance to practice and explore and competence in programming needs time to build.
The list of work for the upcoming week is in Moodle, it is also briefly overviewed in the e-mails sent to the participants through ÕIS. In addition to this work, there may be other tasks or resources available under the current week's material. Please review them, interact with them, but they are not compulsory.
You can always post questions to the course forum. In fact, you are even encouraged to do so - the people are glad to help.