Minu's Games logo
MINU'S ROBOTICS

A Minu's Games EdTech Lab

← Code & Cognition
Day 14 of 30Logic & Data Structures

🤖Function Power & Project

🎯 Today's objective

Use parameters, defaults, scope — then build a guessing bot.

📖 Key concepts

  • Default params: def f(x, step=1)
  • Multiple returns via tuples
  • Local vs global scope
  • random module: randint()

💻 Try this code

import random

secret = random.randint(1, 20)

# Step 1: Function with a default parameter
def check(guess, target=secret):
    if guess == target:
        return "correct"
    return "Guess higher" if guess < target else "Guess lower"

print("Secret Random Number:👀", secret)

# Step 2: Test check function
print("Test check with 10:", check(10))
print("_________")

# Step 3: Function that runs a round & returns a tuple (won, tries)
def play_round(max_tries=3):
    tries = 0
    for _ in range(max_tries):
        g = int(input("Guess (1-20): "))
        tries += 1
        res = check(g)
        print(f"Try {tries}: {g} -> {res}")
        if res == "correct":
            return True, tries
    return False, tries

won, total_tries = play_round()
print(f"Result -> Won: {won}, Tries used: {total_tries}")

👥 Learn it your way

🧒 Young Explorers

Combine everything: loops + ifs + functions = a real guessing bot with stepped scaffolding!

Your task

Run the starter code! Then put the Secret Random Number into the Program Input (stdin) box (or set secret = 15) and add a victory message when won is True!

💼 Professionals

Understand scope and pure functions; isolate randomness for testability (seed it).

Your task

Add difficulty levels (e.g., max_tries=5) to play_round(max_tries=3) and return a result dict {"won": won, "tries": tries}.

🤖 AI connection

This guessing bot uses feedback ("Guess higher/Guess lower") to improve its next guess — a tiny taste of how learning systems use signals to get better.

★ Week 2 Project — Smart Binary-Search Guessing Bot

Write a function auto_bot(target=secret) where the computer finds the secret number automatically without input(): start with low = 1 and high = 20, guess mid = (low + high) // 2, and update low = mid + 1 (if "Guess higher") or high = mid - 1 (if "Guess lower"). Compare how many tries auto_bot() takes vs play_round()!

✅ Quick check

What does random.randint(1,6) simulate?

A dice roll (1–6 inclusive).

What is a default parameter?

A value used when the caller omits that argument.