Arvutiteaduse instituut
  1. Kursused
  2. 2020/21 kevad
  3. Programmeerimine (LTAT.03.001)
EN
Logi sisse

Programmeerimine 2020/21 kevad

  • Home

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 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 (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 37.

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.

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 that is 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

>>> %Run nimi.py
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 autochecker in Moodle, under Homework for Week 2.

NB! Use home2.py, home3.py, home4.py as the names of your files, so that autochecker can find them.

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused