Skip to main content

Chapter 5a - Random

random.randrange(a, b) picks an integer from a up to but not including b.

import random
print(random.randrange(10, 20)) # 10 ... 19
πŸ§ͺ Try it

Mini quiz β€” exclusive stop​

Quiz

Exclusive stop

1 / 2

This call can never be which number?

random.randrange(10, 20)

Pick an answer to see if you are right.

Coin sides​

0 means heads, 1 means tails. Mapping that to words is just if.

Challenge: coin word​

Write coin(n) so it returns Heads if n is 0, otherwise Tails.

Mini quiz β€” randrange​

Quiz

randrange

1 / 2

This call can be which of these?

random.randrange(1, 7)

Pick an answer to see if you are right.

Exercises​

Activity: Team color

Write team(n) so it returns blue if n is 0, otherwise red. (In a real program you would set n = random.randrange(0, 2).)