انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

random: let the program surprise you

Section 11: Randomness and errorsLesson 11.1 · ⏱ 8 min

The random library picks things by chance, and it is the base of every game.

  • random.randint(1, 6) is a number from 1 to 6, like a dice.
  • random.choice(list) is a random item from the list.
  • random.shuffle(list) mixes up the order of the list.
  • random.seed(7) makes “chance” repeat with the same result, which is useful when testing code. Delete it so the result changes every time.
import random

random.seed(7)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
colors = ["red", "green", "blue"]
random.shuffle(colors)
print(colors)
🖥️ The output in Robo Python:
3
rock
['blue', 'red', 'green']
🐍 Open the code in Robo Python ↗
Random choices in Robo Python
Random choices in Robo Python
📌 Note
With random.seed(7) you'll get the same result you see here. Delete that line and run several times.

📌 Another example: rock, paper, scissors

The computer chooses with random.choice. (seed so we get the same result every time.)

When the program asks, we type: rock.

🐍 Rock, paper, scissors
import random
random.seed(2)
me = input("rock, paper or scissors? ")
pc = random.choice(["rock", "paper", "scissors"])
print("computer:", pc)
if me == pc:
    print("draw")
elif (me, pc) in [("rock", "scissors"), ("paper", "rock"), ("scissors", "paper")]:
    print("you win")
else:
    print("you lose")
🖥️ The output in Robo Python:
rock, paper or scissors? rock
computer: rock
draw
🐍 Open the code in Robo Python ↗
✍️ Practise
Delete the seed line and run the program three times. Did the result change?
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Print 5 random numbers between 1 and 100.

👀 Show the solution
🐍 Solution
import random
for i in range(5):
    print(random.randint(1, 100))
🖥️ The output in Robo Python:
12
11
47
22
95
🐍 Open the code in Robo Python ↗

Result: five different numbers every time.

Medium

Choose a random name from the list to start the game.

👀 Show the solution
🐍 Solution
import random
names = ["Ali", "Mona", "Omar", "Sara"]
print(random.choice(names), "starts!")
🖥️ The output in Robo Python:
Omar starts!
🐍 Open the code in Robo Python ↗

Result: a random name, then starts!

Hard

A random password of 6 digits.

👀 Show the solution
🐍 Solution
import random
pw = ""
for i in range(6):
    pw = pw + str(random.randint(0, 9))
print(pw)
🖥️ The output in Robo Python:
493909
🐍 Open the code in Robo Python ↗

Result: six random digits.

Finished the lesson? Mark it so your progress counts.