3. Guess the number
3.1. Random integer
from microbit import *
import random
def get_secret():
return random.randint(1, 9)
Tasks
Add parameters to the get_secret function, min_num and max_num to specify the lowest and highest possible returned integers.
Modify the parameters to have a default of 1 for min_num of 9 for max_num.
Add parameters to the get_secret function, min_num and max_num to specify the lowest and highest possible returned integers.
from microbit import *
import random
def get_secret(min_num, max_num):
return random.randint(min_num, max_num)
Modify the parameters to have a default of 1 for min_num of 9 for max_num.
from microbit import *
import random
def get_secret(min_num=1, max_num=9):
return random.randint(min_num, max_num)
3.2. Select number
3.3. Check the guess
def check_guess(secret_num, guess):
if guess == secret_num:
display.show.........
sleep(400)
return .........
elif guess > secret_num:
display.show(.........)
sleep(400)
display.show(guess)
return .........
else:
display.show(.........)
sleep(400)
display.show(guess)
return .........
Tasks
Complete the code for the check_guess function.
def check_guess(secret_num, guess):
if guess == secret_num:
display.show(Image.YES)
sleep(400)
return True
elif guess > secret_num:
display.show(Image.ARROW_S)
sleep(400)
display.show(guess)
return False
else:
display.show(Image.ARROW_N)
sleep(400)
display.show(guess)
return False
3.4. Guess the number version 1
Tasks
Build Guess the number version 1.
from microbit import *
import random
def get_secret(min_num=1, max_num=9):
return random.randint(min_num, max_num)
def select_number(start_num, min_num=1, max_num=9):
counter = start_num
display.show(counter, delay=200)
while button_b.was_pressed() is False:
if button_a.is_pressed():
counter += 1
if counter > max_num:
counter = min_num
display.show(counter, delay=200)
sleep(200)
return counter
def check_guess(secret_num, guess):
if guess == secret_num:
display.show(Image.YES)
sleep(400)
return True
elif guess > secret_num:
display.show(Image.ARROW_S)
sleep(400)
display.show(guess)
return False
else:
display.show(Image.ARROW_N)
sleep(400)
display.show(guess)
return False
secret_num = get_secret()
guess = 5
guessed = False
display.scroll("1-9?")
while guessed is False:
guess = select_number(guess, min_num=1, max_num=9)
guessed = check_guess(secret_num, guess)
3.5. New versions:
3.6. Guess the number version 2
Tasks
Modify the while-loop so that a new game is automatically started. Just show the main code, excluding functions.
Show the full code.
from microbit import *
import random
secret_num = get_secret()
guess = 5
guessed = False
display.scroll("1-9?")
while True:
guess = select_number(guess, min_num=1, max_num=9)
guessed = check_guess(secret_num, guess)
if guessed:
# new game
secret_num = get_secret()
guess = 5
guessed = False
from microbit import *
import random
def get_secret(min_num=1, max_num=9):
return random.randint(min_num, max_num)
def select_number(start_num, min_num=1, max_num=9):
counter = start_num
display.show(counter, delay=200)
while button_b.was_pressed() is False:
if button_a.is_pressed():
counter += 1
if counter > max_num:
counter = min_num
display.show(counter, delay=200)
sleep(200)
return counter
def check_guess(secret_num, guess):
if guess == secret_num:
display.show(Image.YES)
sleep(400)
return True
elif guess > secret_num:
display.show(Image.ARROW_S)
sleep(400)
display.show(guess)
return False
else:
display.show(Image.ARROW_N)
sleep(400)
display.show(guess)
return False
secret_num = get_secret()
guess = 5
guessed = False
display.scroll("1-9?")
while True:
guess = select_number(guess, min_num=1, max_num=9)
guessed = check_guess(secret_num, guess)
if guessed:
# new game
secret_num = get_secret()
guess = 5
guessed = False
3.7. Guess the number version 3
Tasks
Add counting of the number of guesses made and display it at the end of each game. Just show the main code, excluding functions.
Show the full code.
from microbit import *
import random
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
display.scroll("1-9?")
while True:
guess = select_number(guess, min_num=1, max_num=9)
game_guesses += 1
guessed = check_guess(secret_num, guess)
if guessed:
display.scroll(str(game_guesses) + " GUESSES", delay=80)
# new game
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
from microbit import *
import random
def get_secret(min_num=1, max_num=9):
return random.randint(min_num, max_num)
def select_number(start_num, min_num=1, max_num=9):
counter = start_num
display.show(counter, delay=200)
while button_b.was_pressed() is False:
if button_a.is_pressed():
counter += 1
if counter > max_num:
counter = min_num
display.show(counter, delay=200)
sleep(200)
return counter
def check_guess(secret_num, guess):
if guess == secret_num:
display.show(Image.YES)
sleep(400)
return True
elif guess > secret_num:
display.show(Image.ARROW_S)
sleep(400)
display.show(guess)
return False
else:
display.show(Image.ARROW_N)
sleep(400)
display.show(guess)
return False
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
display.scroll("1-9?")
while True:
guess = select_number(guess, min_num=1, max_num=9)
game_guesses += 1
guessed = check_guess(secret_num, guess)
if guessed:
display.scroll(str(game_guesses) + " GUESSES", delay=80)
# new game
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
3.8. Guess the number version 4
Tasks
Write a function, get_best_score, that gets the lower value from the current best_score and game_guesses from a finished game. Use an if statement to first check whether best_score is None.
from microbit import *
import random
def get_best_score(best_score, game_guesses):
if best_score is None:
return game_guesses
else:
return min(best_score, game_guesses)
Tasks
Add tracking of the total number of games played and the best score. Just show the main code.
Show the full code.
from microbit import *
import random
def get_best_score(best_score, game_guesses):
if best_score is None:
return game_guesses
else:
return min(best_score, game_guesses)
total_games = 1
best_score = None
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
display.scroll("1-9?")
while True:
guess = select_number(guess, min_num=1, max_num=9)
game_guesses += 1
guessed = check_guess(secret_num, guess)
if guessed:
display.scroll(str(game_guesses) + " GUESSES", delay=80)
best_score = get_best_score(best_score, game_guesses)
display.scroll("BEST: " + str(best_score) + " GAMES: " + str(total_games), delay=80)
# new game
total_games += 1
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
from microbit import *
import random
def get_secret(min_num=1, max_num=9):
return random.randint(min_num, max_num)
def select_number(start_num, min_num=1, max_num=9):
counter = start_num
display.show(counter, delay=200)
while button_b.was_pressed() is False:
if button_a.is_pressed():
counter += 1
if counter > max_num:
counter = min_num
display.show(counter, delay=200)
sleep(200)
return counter
def check_guess(secret_num, guess):
if guess == secret_num:
display.show(Image.YES)
sleep(400)
return True
elif guess > secret_num:
display.show(Image.ARROW_S)
sleep(400)
display.show(guess)
return False
else:
display.show(Image.ARROW_N)
sleep(400)
display.show(guess)
return False
def get_best_score(best_score, game_guesses):
if best_score is None:
return game_guesses
else:
return min(best_score, game_guesses)
total_games = 1
best_score = None
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False
display.scroll("1-9?")
while True:
guess = select_number(guess, min_num=1, max_num=9)
game_guesses += 1
guessed = check_guess(secret_num, guess)
if guessed:
display.scroll(str(game_guesses) + " GUESSES", delay=80)
best_score = get_best_score(best_score, game_guesses)
display.scroll("BEST: " + str(best_score) + " GAMES: " + str(total_games), delay=80)
# new game
total_games += 1
secret_num = get_secret()
guess = 5
game_guesses = 0
guessed = False