Week 9

Surprise! Random Choices

Aim: Make Python choose numbers by chance.

New coding words

  1. random
  2. import
  3. randint
  4. elif
  5. chance

What the coding words mean

  1. random: Chosen without knowing beforehand what the result will be.
  2. import: Makes another Python toolbox available to the program.
  3. randint: Chooses a random integer between two given numbers.
  4. elif: Means 'else if' and checks another condition.
  5. chance: Something that may produce a different result each time.
Code along

Roll a random number

Roll a six-sided die and choose a message based on its value.

Python coding spaceReady to run
.py

Tab indents. Esc then Tab leaves the editor. Ctrl/⌘ + Enter runs your code.

Output (Shell)What your program does
Select Run my code to try this program.
Opening your coding space…Each activity has its own saved code.
Mini project

Dice Battle

Roll for a player and computer, then use conditions to choose the winner.

Python coding spaceReady to run
.py

Tab indents. Esc then Tab leaves the editor. Ctrl/⌘ + Enter runs your code.

Output (Shell)What your program does
Select Run my code to try this program.
Opening your coding space…Each activity has its own saved code.

Your mini-project to-do list

0 of 4 completed

Complete these activities in the mini-project editor above. After each change, select Run my code to check your program’s output or drawing.

Save your finished mini project

Select Download my work and save the file as week_9_dice_battle.py. The .py ending tells us that the file contains Python code. Keep this file as a copy of your finished program.

Debugging detective

Chi Opara, your course guide

Knowledge check

Question 1
Python coderandom.randint(1, 6)

Can this choose the number 6?

Choose one answer
Question 2

Why must we write import random before using random.randint()?

Your answers are checked in this browser and are not submitted.

How are you getting on so far?

My checklist

Finished Week 9?

This progress is saved only in this browser.