In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.
This is the dice simulator from the predict and run task. Run it a few times before you start.
import random
print("Welcome to the dice simulator!")
num1 = random.randint(1, 6)
print("You rolled a " + str(num1))
What is the term for the (1, 6) values used by the randint function?
They are arguments: values handed to a function inside its brackets.
randint needs two of them. The first is the lowest number it may pick and the
second is the highest, so these two arguments are what make this a six-sided
dice rather than any other kind.
Why are the numbers in brackets not (0, 6)?
Because a dice has no 0 on it.
randint includes both of its arguments. random.randint(0, 6) could pick 0,
1, 2, 3, 4, 5 or 6: seven possible numbers, one of which no real dice can roll.
Change line 4 to (0, 6) and keep running the program, and sooner or later it
tells you that you rolled a 0.
This is easy to get wrong if you are thinking of range, where the count
starts at 0 and the stop value is left out. randint does neither: it starts
where you tell it to and stops exactly where you tell it to.
Give the line number where variable assignment is used.
Line 4, num1 = random.randint(1, 6).
The = stores the number randint picks in the variable num1, so that line 5
can use it. It is the only line in the program with an = in it.
Give the line number where casting is used.
Line 5, where str(num1) turns the number into text.
randint gives back a whole number, and a whole number cannot be joined onto
"You rolled a " with a +. Take the str() away and Python stops with a
TypeError instead of printing the roll.
Make sure that you check for the following things:
import random is the first line of the program.randint is written after random and a full stop: random.randint(...).random.randint(1, 6) includes the 6.str() before it is joined onto text with a
+.