Tutorial for first year computer science students

Back to Tutorials

Dice Game - Multiple Branching

Learning Objectives

We are going to learn:

  1. Multiple branching with elif statements.
  2. How to use nested if statements.

Exercises

Download the Medialib ex5 code files and 7 image files (as shown below) from the gallery and complete the program in ex5.py.

MediaLib Dice Exercise

Create a program with MediaLib to realise a simple dice game by modifying the code from the previous exercise for drawing a dice face:

  1. Click the window of MediaLib to roll the 1st dice (generate a random integer in [1, 6]) and draw the dice face on the left part of the window.
  2. Click the window to roll the 2nd dice and draw the dice face on the right part of the window.
  3. Click the window to check which side wins. Draw a gold medal (medal1.png) under the winner and a silver medal (medal2.png) for the other. If it is a tied game, then draw two gold medals.
MediaLib Dice Game

Hints:

  • To generate a random integer, use the function randint(a,b) from the random module which will return an integer in the range [a,b].
  • You can modify the function draw_dice_face(n) created in the previous exercise to, for example, draw_dice_face(n, side).
  • You can create a function draw_medal(place, side) for drawing one of the medals.



Creative Commons Licence
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.