Institute of Computer Science
  1. Courses
  2. 2022/23 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2022/23 fall

  • Home
  • Grading
  • Links

Homework 4

The deadline is Thursday, the 29th of September, 23:59 (Estonian time).

Homework 4.1 Rock paper scissors

Write a program that lets the user play rock-paper-scissors against the computer. It should let the user choose between 2 options: play or quit. In the case of playing, the user must be able to choose between rock, paper, or scissors. However, the computer is broken and always chooses paper.

Your program should display the option that was picked by the computer as well as the result of the game (player's win, loss or draw).

Optional bonus task: Fix the computer so it picks randomly between all 3 options (rock, paper, scissors). Hint: For the computer’s choice, use random.choice().

Example of modified program where the computer chooses randomly:

>>> %Run solution.py
Type S to stop or P to play rock paper scissors: P
Choose rock, paper or scissors: paper
Computer chose paper
Draw
Type S to stop or P to play rock paper scissors: P
Choose rock, paper or scissors: paper
Computer chose scissors
You lost
Type S to stop or P to play rock paper scissors: S
Game ended

If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.

Homework 4.2 Sum of squares

Write a function squareSum that takes one integer as an argument and returns the sum of all integer squares from 1 to the argument value (included).

For example, if the argument is 3, then the function should calculate as follows: {$ 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 $}

Examples of function calls:

>>> squareSum(3)
14

>>> squareSum(10)
385

Modify the program to get the integer from user input, call the function with it and display the final result.

Examples of program output:

>>> %Run solution.py
Please enter an integer: 3
The sum of integer squares from 1 to 3 is 14

>>> %Run solution.py
Please enter an integer: 10
The sum of integer squares from 1 to 10 is 385

If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.

Go to Moodle and upload your solution into Homework 4.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment