Python KPRIDE: Random Numbers

Last updated 6 months ago
4 questions

For GCSE, you must know the following for programming:

  • Outputs and Inputs
  • Variables
  • Maths Operators
  • Random numbers
  • Strings and Casting
  • Booleans
  • If, elif, else
  • Nested Ifs
  • For loops
  • While loops
  • Nested loops
  • Arrays/Lists
  • 2D Lists
  • File handling
  • Subprograms(functions/procedures)

Warm-up Practice:
https://type.withcode.uk/python/Variables_and_Constants

Learning Objectives:

  • I can tell someone what an algorithm is
  • I know how to create a simple Python Program (Input and Output)
  • I can explain and debug an algorithm/Python Program

Keywords

  1. Sequence: Lines of code are run in order they appear once.
  2. Integer: a whole number; a number that is not a fraction.
  3. Float / Real: A number with a decimal point
  4. Random number: A number that appears to have been selected randomly from a set
  5. Import random: Let's you use a library of code that focuses on random numbers

Predict and Run

Look at the code for each question.
What do you think will be the output?
What is the actual output?
1

What do you think will happen with this code:

import random randomNumber = random.randint(1,6) print("You rolled a ...") print(randomNumber)

Investigate

  1. Look at the code on the right.
  2. Run it and see what it does.
  3. Click on the pencil icon at the top to see the code again.
  4. Change the numbers inside randint( ) and see what happens.
  5. Does it still work if the number 1 is replaced?

Debug

Click on each link and try dragging and dropping code into the correct order
https://7strikelink7.github.io/NormalPythonParsons/parsons/Random.html

Extend

Help:
print ( ) is used to output text
input( ) is used to get the user to type something

Variables store values that can change.
Name = Value here
e.g. myName = "John"

To store numbers in a variable use:
variableName = int(input())
1

Create a program that will ask the user for how many sides they want their dice to have using:
int(input(" "))
Then generate a number from 1 to that number.

1

Create a program that generates two random numbers between 1 and 6.

Add both of these numbers together and display the total.

1

Continue with the last code:
Generate a third roll from 2 to 12.
If that number is larger than the sum of the first two numbers, output "You lose". If it is lower, output "You win!".
If it is the same, output "Draw".