In a predict and run task you do not write code from scratch. You start from a small piece of working code, work out what you think it will do before you run it, and then run it to check whether you were right.
Random numbers change what “right” means. You cannot predict the exact number a random program will print, because nobody can. What you can predict is every number it might print, and every number it never will. That is what the prompts below ask for.
For every example, study the code carefully and write down a prediction before
you touch the Run button. Your prediction should use the key terms random,
import and argument, and it should list the possible outputs: the
lowest, the highest, and anything in between that can never come up. You can
type your prediction straight into the code as a comment, on a line starting
with #.
Only once you have written your prediction should you run the code, several times, and compare the results with what you expected.
import random
print(random.randint(1, 5))
num1 = random.randint(1, 10)
print("Your number was " + str(num1))
This prints two lines. Predict the smallest and the largest number each line can show, and whether the two numbers have to be different from each other.
import random
print("Welcome to the dice simulator!")
num1 = random.randint(1, 6)
print("You rolled a " + str(num1))
Predict which of the three lines is the same on every run and which one changes. Then run it until you have seen both a 1 and a 6.
import random die = [1, 2, 3, 4, 5, 6] num = random.choice(die) print(num)
This is a dice roll too, written with choice instead of randint. Predict
whether it can print anything Example 2 could not, and what would change if the
6 in the list were replaced with a 7.
from random import randrange my_number = randrange(0, 10, 2) print(my_number)
Predict every number this can print. Then run it five times and write down the output each time. What do you notice about the five outputs? Do they have anything in common?
Now change line 3 of the code in Example 4 so that it reads:
my_number = randrange(1, 10, 2)
Before you run the code again, write down every output you think is possible now. Then run it several times and check each result against your list.
With randrange(0, 10, 2) every output is even: 0, 2, 4, 6 or 8. The range
starts at 0 and goes up in twos, so it jumps over every odd number, and it
never reaches 10.
Starting at 1 instead moves every value up by one, so randrange(1, 10, 2)
prints only odd numbers: 1, 3, 5, 7 or 9. The stop value is still 10 and still
never reached, but here that makes no difference, because 10 is not odd.
Make sure that you check for the following things:
import random or from random import ... is the first line of the program.import random, every tool is written with random. in front of it.from random import randrange, it is written as a bare randrange.randint includes both of its numbers, while randrange never picks its stop
value.str() before it is joined onto text with a
+.