Practice and reinforce the concepts from Lesson 9
random
package to generate random numberstime
package to create delays and timers:information_source: :computer: Coding Activity | Estimated time: 45-60 minutes
Click the link here and make a copy of Colab Notebook into your Google Drive for coding.
Write the program using packages to show the outcome for each question.
⏱️ Time: 10 minutes
Using package random and loop, generate 5 random numbers from 1 to 20:
random
package at the beginning of your coderandom.randint(1, 20)
to generate a random number:bulb: :bulb: Remember:
randint(a, b)
includes both endpoints, sorandint(1, 20)
can return 1 or 20!
Expected output:
output19 7 18 5 13
Question 2: Timer with Delays
⏱️ Time: 10 minutes
Using package time and loop, count from 1 to 10 every second.
- Import the
time
package- Create a for loop using
range(1, 11)
to count from 1 to 10- Inside the loop:
- Print the current number
- Use
time.sleep(1)
to pause for 1 secondExpected output:
tip :bulb: Bonus Challenge: Try counting backwards! Use
range(10, 0, -1)
or thereversed()
function to countdown from 10 to 1.
⏱️ Time: 20-25 minutes
Create an interactive guessing game using random
, loops, and conditionals:
Generate the secret number:
random
packagerandom.randint(1, 100)
to generate a numberanswer
Create the game loop:
while True:
loop to keep asking for guessesinput("Guess who am I: ")
to get the user's guessint()
Check the guess:
break
the loop:bulb: :bulb: Common mistake: Don't forget to convert the input to
int()
! Otherwise, you'll be comparing text to a number.
Game Flow Diagram:
python
+--------------------------------+ | Random number from 1 to 100 | +--------------------------------+ | ▼ +---------------------------------+ -->| Ask user to guess by |<-- | | using input | | | +---------------------------------+ | | / | \ | | / | \ | | "guess is | "guess is | | smaller" | bigger" | | | | | | | ▼ | ▼ | | +-------------+ | +-------------+ | | | Your guess | | | Your guess | | | | is too small| | | is too big | | | +-------------+ | +-------------+ | | | | | | | "Ask for | "Ask for | ---- new guess" | new guess"----- | "guess is correct" | ▼ +----------------------------+ | Awesome! Your | | guess is correct. | +----------------------------+
Bonus Challenges
⏱️ Time: 15-20 minutes (optional)
:star2: Challenge One: Dynamic Range Display
Improve your game by showing the valid range after each guess:
- Keep track of the lowest and highest valid numbers
- After each guess:
- If guess is too small: update minimum and show "Range from [guess] to [maximum]"
- If guess is too big: update maximum and show "Range from [minimum] to [guess]"
:star2: Challenge 2: Multiplayer Mode
Make it a competitive game:
- Ask "How many players?" at the start
- Create a list to track player names
- Players take turns guessing
- Display "Player [X]'s turn" before each guess
- Announce which player won! tip :bulb: Use the modulo operator
%
to cycle through players!
"NameError: name 'random' is not defined"
import random
at the top of your code"ValueError: invalid literal for int()"
Game runs too fast/slow
sleep()
time in Question 2Infinite loop in the guessing game
break
statement when the guess is correct:warning: Warning :memo: Important: Before submitting, make sure:
- All three questions are completed
- Your code runs without errors
- You've tested the guessing game at least once
- Save your Colab notebook!
Make sure to review your answers before submitting. Your responses will help track your progress.
:link: Submit your exercise here