<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Choose your own adventure &#8211; sekol.ninja</title>
	<atom:link href="https://sekol.ninja/tag/choose-your-own-adventure/feed/" rel="self" type="application/rss+xml" />
	<link>https://sekol.ninja</link>
	<description></description>
	<lastBuildDate>Wed, 10 Jun 2026 13:43:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>Build Your Own AI-Powered Choose Your Own Adventure</title>
		<link>https://sekol.ninja/build-your-own-ai-powered-choose-your-own-adventure/</link>
		
		<dc:creator><![CDATA[Michael Sekol]]></dc:creator>
		<pubDate>Wed, 10 Jun 2026 12:02:53 +0000</pubDate>
				<category><![CDATA[Learning to Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[ChatGPT]]></category>
		<category><![CDATA[Choose your own adventure]]></category>
		<category><![CDATA[Learn to Code]]></category>
		<category><![CDATA[Summer Camp]]></category>
		<guid isPermaLink="false">https://sekol.ninja/?p=10640</guid>

					<description><![CDATA[A Step-by-Step Python Guide for Beginners In this project, you will write a Python program that uses real artificial intelligence (ChatGPT) to [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h3 class="wp-block-heading">A Step-by-Step Python Guide for Beginners</h3>



<p class="wp-block-paragraph">In this project, you will write a Python program that uses real artificial intelligence</p>



<p class="wp-block-paragraph">(ChatGPT) to create a story that changes based on YOUR choices.</p>



<p class="wp-block-paragraph">Every time you play, the story will be different!</p>



<p class="wp-block-paragraph"><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-global-color-2-color">No coding experience needed — just follow the steps!</mark></p>



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
  /* Reset for WordPress compatibility */
  .adventure-guide * { box-sizing: border-box !important; }
  .adventure-guide {
    font-family: 'Segoe UI', Arial, sans-serif !important;
    max-width: 860px !important;
    margin: 0 auto !important;
    padding: 20px !important;
    color: #333 !important;
    line-height: 1.7 !important;
    font-size: 16px !important;
  }
  .adventure-guide h1 {
    color: #1B365D !important;
    font-size: 2em !important;
    border-bottom: 3px solid #2E75B6 !important;
    padding-bottom: 10px !important;
    margin-top: 40px !important;
    margin-bottom: 20px !important;
  }
  .adventure-guide h2 {
    color: #2E75B6 !important;
    font-size: 1.5em !important;
    margin-top: 35px !important;
    margin-bottom: 15px !important;
  }
  .adventure-guide h3 {
    color: #333 !important;
    font-size: 1.2em !important;
    margin-top: 25px !important;
    margin-bottom: 10px !important;
  }
  .adventure-guide p {
    margin: 10px 0 !important;
    line-height: 1.7 !important;
  }
  .adventure-guide .hero-section {
    text-align: center !important;
    padding: 40px 20px !important;
    margin-bottom: 30px !important;
    background: linear-gradient(135deg, #1B365D, #2E75B6) !important;
    color: white !important;
    border-radius: 12px !important;
  }
  .adventure-guide .hero-section h1 {
    color: white !important;
    border-bottom: none !important;
    font-size: 2.2em !important;
    margin: 0 0 10px 0 !important;
  }
  .adventure-guide .hero-section p {
    color: rgba(255,255,255,0.9) !important;
    font-size: 1.1em !important;
    margin: 5px 0 !important;
  }
  /* Callout boxes */
  .adventure-guide .box {
    border-radius: 8px !important;
    padding: 18px 22px !important;
    margin: 16px 0 !important;
    border-left: 5px solid #ccc !important;
  }
  .adventure-guide .box-title {
    font-weight: bold !important;
    margin-bottom: 8px !important;
    color: #1B365D !important;
    font-size: 1.05em !important;
  }
  .adventure-guide .box-purple {
    background-color: #F3E5F5 !important;
    border-left-color: #9C27B0 !important;
  }
  .adventure-guide .box-green {
    background-color: #E8F5E9 !important;
    border-left-color: #4CAF50 !important;
  }
  .adventure-guide .box-orange {
    background-color: #FFF3E0 !important;
    border-left-color: #FF9800 !important;
  }
  .adventure-guide .box-red {
    background-color: #FFEBEE !important;
    border-left-color: #F44336 !important;
  }
  .adventure-guide .box-teal {
    background-color: #E0F7FA !important;
    border-left-color: #00BCD4 !important;
  }
  .adventure-guide .box-blue {
    background-color: #E8F0FE !important;
    border-left-color: #2E75B6 !important;
  }
  /* Code blocks */
  .adventure-guide pre {
    background-color: #1E1E1E !important;
    color: #D4D4D4 !important;
    border-radius: 8px !important;
    padding: 18px 22px !important;
    margin: 16px 0 !important;
    overflow-x: auto !important;
    font-family: 'Courier New', monospace !important;
    font-size: 14px !important;
    line-height: 1.6 !important;
    border: 1px solid #333 !important;
  }
  .adventure-guide pre .comment { color: #6A9955 !important; }
  .adventure-guide pre .string { color: #CE9178 !important; }
  .adventure-guide pre .keyword { color: #569CD6 !important; }
  .adventure-guide pre .function { color: #DCDCAA !important; }
  .adventure-guide pre .number { color: #B5CEA8 !important; }
  /* Inline code */
  .adventure-guide code {
    background-color: #f0f0f0 !important;
    color: #333 !important;
    padding: 2px 6px !important;
    border-radius: 4px !important;
    font-family: 'Courier New', monospace !important;
    font-size: 0.9em !important;
  }
  .adventure-guide pre code {
    background: none !important;
    color: inherit !important;
    padding: 0 !important;
    border-radius: 0 !important;
    font-size: inherit !important;
  }
  /* Terminal commands */
  .adventure-guide .terminal-cmd {
    background-color: #E8F5E9 !important;
    border-left: 5px solid #4CAF50 !important;
    padding: 12px 18px !important;
    border-radius: 8px !important;
    margin: 12px 0 !important;
    font-family: 'Courier New', monospace !important;
    font-size: 15px !important;
    color: #333 !important;
  }
  /* Step headers */
  .adventure-guide .step-header {
    display: flex !important;
    align-items: baseline !important;
    gap: 10px !important;
    margin-top: 35px !important;
    margin-bottom: 15px !important;
    padding-bottom: 8px !important;
    border-bottom: 2px solid #E8F0FE !important;
  }
  .adventure-guide .step-num {
    color: #2E75B6 !important;
    font-size: 1.6em !important;
    font-weight: bold !important;
    white-space: nowrap !important;
  }
  .adventure-guide .step-title {
    color: #1B365D !important;
    font-size: 1.6em !important;
    font-weight: bold !important;
  }
  /* Color key grid */
  .adventure-guide .color-key {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
    margin: 20px 0 !important;
  }
  @media (max-width: 600px) {
    .adventure-guide .color-key { grid-template-columns: 1fr !important; }
    .adventure-guide .step-header { flex-direction: column !important; gap: 2px !important; }
  }
  .adventure-guide .color-key .box { margin: 0 !important; }
  /* Table */
  .adventure-guide table {
    width: 100% !important;
    border-collapse: collapse !important;
    margin: 16px 0 !important;
    font-size: 0.95em !important;
  }
  .adventure-guide th {
    background-color: #1B365D !important;
    color: white !important;
    padding: 10px 14px !important;
    text-align: left !important;
    font-weight: bold !important;
  }
  .adventure-guide td {
    padding: 10px 14px !important;
    border: 1px solid #ddd !important;
    vertical-align: top !important;
  }
  .adventure-guide tr:nth-child(even) td { background-color: #f5f8ff !important; }
  .adventure-guide ul { margin: 8px 0 !important; padding-left: 24px !important; }
  .adventure-guide li { margin: 4px 0 !important; }
  .adventure-guide a { color: #2E75B6 !important; }
</style>
</head>
<body>
<div class="adventure-guide">

<!-- HERO -->
<div class="hero-section">
  <h1>In this project, you will write a Python program that uses real artificial intelligence (ChatGPT) to create a story that changes based on <strong>your</strong> choices. Every time you play, the story will be different!</h1>
  <p style="margin-top: 15px !important; font-style: italic !important; opacity: 0.85 !important;"></p>
</div>

<!-- COLOR KEY -->
<h1>How to Read This Guide</h1>
<p>This guide uses color-coded boxes to help you know what you&#8217;re looking at:</p>

<div class="color-key">
  <div class="box box-purple"><div class="box-title">🟣 PURPLE — New Vocabulary</div>Explains new words or concepts.</div>
  <div class="box box-green"><div class="box-title">🟢 GREEN — Terminal Commands</div>Commands for the terminal, NOT Thonny.</div>
  <div class="box box-orange"><div class="box-title">🟠 ORANGE — Tips &#038; Hints</div>Helpful tips and suggestions.</div>
  <div class="box box-red"><div class="box-title">🔴 RED — Warnings &#038; Mistakes</div>Common errors and how to fix them.</div>
</div>
<div class="color-key" style="margin-top: 0 !important;">
  <div class="box box-teal"><div class="box-title">🟦 TEAL — Expected Output</div>What you should see on screen.</div>
  <div class="box" style="background: #1E1E1E !important; border-left-color: #555 !important; color: #D4D4D4 !important;"><div class="box-title" style="color: #D4D4D4 !important;">⬛ DARK — Code to Type</div>Type this exactly into Thonny.</div>
</div>

<!-- WHAT YOU NEED -->
<h1>What You Need Before Starting</h1>
<ul>
  <li><strong>A Raspberry Pi</strong> that is turned on, connected to a monitor, keyboard, mouse, and the internet (Wi-Fi).</li>
  <li><strong>Thonny</strong> — the program where you write your code. It&#8217;s already installed on every Raspberry Pi. Look for a blue and white icon on the desktop or in the menu under <strong>Programming</strong>.</li>
  <li><strong>An API key</strong> — a special password that lets your program talk to ChatGPT. You&#8217;ll copy and paste it from <a href="https://www.sekol.ninja" target="_blank">www.sekol.ninja</a>.</li>
</ul>

<div class="box box-purple">
  <div class="box-title">🟣 New Vocabulary: What is an API Key?</div>
  <p>An <strong>API key</strong> is like a special password that lets one computer program talk to another. In this project, your Python program needs to talk to ChatGPT (which lives on the internet). The API key proves to ChatGPT that you have permission to use it.</p>
  <p>Think of it like a library card — the library (ChatGPT) won&#8217;t let you borrow books (get AI responses) unless you show your card (API key).</p>
</div>

<!-- STEP 1 -->
<div class="step-header"><span class="step-num">Step 1:</span><span class="step-title">Open the Terminal</span></div>

<p>First, we need to install a small piece of software that lets Python talk to ChatGPT. To do this, we&#8217;ll use the <strong>terminal</strong> — the black window where you type commands directly to the computer.</p>
<p>Click the <strong>black rectangle icon</strong> in the top taskbar of your Raspberry Pi. A black window with a blinking cursor should open.</p>

<div class="box box-purple">
  <div class="box-title">🟣 New Vocabulary: What is the Terminal?</div>
  <p>The <strong>terminal</strong> (also called the &#8220;command line&#8221;) is a way to talk to your computer by typing text commands instead of clicking buttons. It&#8217;s what hackers use in movies! But in real life, it&#8217;s just a tool that programmers and cybersecurity experts use every day.</p>
</div>

<!-- STEP 2 -->
<div class="step-header"><span class="step-num">Step 2:</span><span class="step-title">Install the OpenAI Library</span></div>

<p>Type the following command into the terminal and press <strong>Enter</strong>:</p>
<div class="terminal-cmd">pip install openai</div>
<p>Wait for it to finish. You&#8217;ll see some text scroll by as it downloads and installs. When you see the blinking cursor again, it&#8217;s done.</p>

<div class="box box-purple">
  <div class="box-title">🟣 New Vocabulary: What is a Library?</div>
  <p>A <strong>library</strong> (in coding) is a bundle of pre-written code that someone else made for you. Instead of writing hundreds of lines of code to connect to ChatGPT yourself, you just install the <code>openai</code> library and it handles all the complicated stuff. Think of it like using a recipe from a cookbook instead of inventing a dish from scratch.</p>
</div>

<div class="box box-red">
  <div class="box-title">🔴 If You See an Error</div>
  <p>If you see a red error message, try this command instead:</p>
  <div class="terminal-cmd" style="background-color: #ffcdd2 !important; border-left-color: #c62828 !important;">pip install openai &#8211;break-system-packages</div>
  <p>This is a Raspberry Pi-specific thing. It&#8217;s safe and normal.</p>
</div>

<!-- STEP 3 -->
<div class="step-header"><span class="step-num">Step 3:</span><span class="step-title">Open Thonny and Create a New File</span></div>

<p>Now close the terminal (type <code>exit</code> or click the X). Open <strong>Thonny</strong> by clicking its icon (blue and white logo) on the desktop, or find it in the Raspberry Pi menu under <strong>Programming → Thonny</strong>.</p>
<p>When Thonny opens, you&#8217;ll see two sections:</p>
<ul>
  <li><strong>Top section</strong> — this is where you write your code (the &#8220;editor&#8221;)</li>
  <li><strong>Bottom section</strong> — this is where your program&#8217;s output appears (the &#8220;shell&#8221;)</li>
</ul>
<p>Click <strong>File → Save As</strong>. Name your file <code>adventure.py</code> and save it to the Desktop.</p>

<div class="box box-red">
  <div class="box-title">🔴 Important: The File Name Must End in .py</div>
  <p>Python files always end in <code>.py</code> — this tells the computer it&#8217;s a Python program. Make sure you type <code>adventure.py</code> (not just <code>adventure</code>).</p>
</div>

<!-- STEP 4 -->
<div class="step-header"><span class="step-num">Step 4:</span><span class="step-title">Import the Library</span></div>

<p>Now we start writing code! In the <strong>top section</strong> of Thonny (the editor), type the following:</p>

<pre><code><span class="comment"># ============================================</span>
<span class="comment"># CHOOSE YOUR OWN ADVENTURE</span>
<span class="comment"># Powered by AI (ChatGPT)</span>
<span class="comment"># ============================================</span>

<span class="comment"># Import the library we need</span>
<span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p>Lines that start with <code>#</code> are called <strong>comments</strong>. The computer completely ignores them. They&#8217;re notes for humans reading the code.</p>
  <p>The line <code>from openai import OpenAI</code> tells Python: &#8220;Go find the openai library we installed, and bring in the tool called OpenAI so we can use it.&#8221;</p>
</div>

<!-- STEP 5 -->
<div class="step-header"><span class="step-num">Step 5:</span><span class="step-title">Connect to ChatGPT</span></div>

<p>Press <strong>Enter</strong> twice after the last line (to leave a blank line), then type:</p>

<pre><code><span class="comment"># Set up the connection to ChatGPT</span>
<span class="comment"># Get your API key from www.sekol.ninja</span>
client = OpenAI(api_key=<span class="string">"paste-your-api-key-here"</span>)</code></pre>

<div class="box box-orange">
  <div class="box-title">❗ Getting Your API Key</div>
  <p>You need to replace <code>paste-your-api-key-here</code> with a real API key.</p>
  <p><strong>Go to <a href="https://www.sekol.ninja" target="_blank">www.sekol.ninja</a> in your web browser and copy the API key from the page.</strong></p>
  <p>Then come back to Thonny and paste it between the quotation marks. It will look something like:<br>
  <code>client = OpenAI(api_key="sk-abc123xyz456...")</code></p>
  <p><strong>Keep the quotation marks!</strong> The key must be inside the quotes.</p>
</div>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p>This line creates a <strong>client</strong> — think of it as a phone that can call ChatGPT. The API key is like the phone number. Without it, you can&#8217;t make the call.</p>
</div>

<!-- STEP 6 -->
<div class="step-header"><span class="step-num">Step 6:</span><span class="step-title">Give the AI Its Personality</span></div>

<p>This is the fun part! We&#8217;re going to tell ChatGPT what kind of storyteller it should be. Add this code below what you already have:</p>

<pre><code><span class="comment"># Tell the AI what kind of stories to create</span>
system_message = {
    <span class="string">"role"</span>: <span class="string">"system"</span>,
    <span class="string">"content"</span>: <span class="string">"""You are an adventure story master for kids.
You create exciting choose-your-own-adventure stories.

RULES:
- Keep the story fun, exciting, and kid-friendly.
- Each response should be 2-3 short paragraphs.
- Always end with exactly 3 numbered choices for what to do next.
- Use vivid descriptions to make the story come alive.
- The story should have a sci-fi space adventure theme.
- Never include anything scary, violent, or inappropriate."""</span>
}</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p>This creates a <strong>system message</strong> — it&#8217;s like a set of instructions you whisper to ChatGPT before the conversation starts. The player never sees this, but it controls HOW the AI tells the story.</p>
  <p><code>"role": "system"</code> tells ChatGPT &#8220;these are your instructions, not something the user said.&#8221;</p>
  <p>The three quotation marks <code>"""</code> let you write a message that spans multiple lines.</p>
</div>

<div class="box box-orange">
  <div class="box-title">🟠 Customize the Theme!</div>
  <p>Want a fantasy adventure instead of sci-fi? A mystery? A pirate story? Change the RULES section to whatever you want! This is YOUR story engine.</p>
</div>

<!-- STEP 7 -->
<div class="step-header"><span class="step-num">Step 7:</span><span class="step-title">Create the Conversation History</span></div>

<p>Add this line below the system message:</p>

<pre><code><span class="comment"># Create a list to keep track of the conversation</span>
conversation = [system_message]</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p>ChatGPT doesn&#8217;t have a memory on its own. Every time you send it a message, you have to send the <strong>entire conversation so far</strong>. This list called <code>conversation</code> stores every message — both yours and the AI&#8217;s.</p>
  <p>Right now it only contains the system message. As the game goes on, we&#8217;ll add more messages to this list.</p>
</div>

<!-- STEP 8 -->
<div class="step-header"><span class="step-num">Step 8:</span><span class="step-title">Create the Welcome Screen</span></div>

<p>Add a welcome screen that the player sees when they start:</p>

<pre><code><span class="comment"># Print the welcome screen</span>
<span class="function">print</span>(<span class="string">"="</span> * <span class="number">50</span>)
<span class="function">print</span>(<span class="string">"  CHOOSE YOUR OWN ADVENTURE"</span>)
<span class="function">print</span>(<span class="string">"  Powered by AI"</span>)
<span class="function">print</span>(<span class="string">"="</span> * <span class="number">50</span>)
<span class="function">print</span>()</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p><code>print()</code> displays text on the screen. <code>"=" * 50</code> means &#8220;repeat the = sign 50 times&#8221; — it makes a nice decorative line. <code>print()</code> with nothing inside prints a blank line for spacing.</p>
</div>

<!-- STEP 9 -->
<div class="step-header"><span class="step-num">Step 9:</span><span class="step-title">Ask for the Player&#8217;s Name</span></div>

<pre><code><span class="comment"># Ask the player for their name</span>
player_name = <span class="function">input</span>(<span class="string">"Enter your character's name: "</span>)
<span class="function">print</span>()
<span class="function">print</span>(<span class="string">"Welcome, "</span> + player_name + <span class="string">"! Your adventure begins..."</span>)
<span class="function">print</span>()</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p><code>input()</code> pauses the program and waits for the player to type something. Whatever they type gets stored in <code>player_name</code>.</p>
  <p>The <code>+</code> sign connects pieces of text together. So if the player types &#8220;Luna&#8221;, it prints: <em>Welcome, Luna! Your adventure begins&#8230;</em></p>
</div>

<!-- STEP 10 -->
<div class="step-header"><span class="step-num">Step 10:</span><span class="step-title">Send the First Message</span></div>

<pre><code><span class="comment"># Create the first message to start the story</span>
first_message = {
    <span class="string">"role"</span>: <span class="string">"user"</span>,
    <span class="string">"content"</span>: <span class="string">"Start a new adventure story. My character's name is "</span> + player_name + <span class="string">". Begin the story!"</span>
}
conversation.append(first_message)</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do?</div>
  <p>This creates a message with <code>"role": "user"</code> — meaning it&#8217;s FROM the player TO the AI.</p>
  <p><code>.append()</code> means &#8220;add to the end of the list.&#8221; Now our <code>conversation</code> list has two items: the system message AND this first user message.</p>
</div>

<!-- STEP 11 -->
<div class="step-header"><span class="step-num">Step 11:</span><span class="step-title">Build the Game Loop</span></div>

<p>This is the biggest section — the engine that makes the whole game run. It&#8217;s a <strong>loop</strong> that keeps going: send a message to ChatGPT → show the story → ask the player → repeat.</p>

<p><strong>⚠️ Indentation (the spaces at the beginning of lines) is very important in Python.</strong> Everything inside the loop must be indented with 4 spaces. Thonny should add the spaces automatically when you press Enter after a line ending with <code>:</code></p>

<pre><code><span class="comment"># The game loop</span>
<span class="keyword">while</span> True:
    <span class="comment"># Send the conversation to ChatGPT</span>
    response = client.chat.completions.create(
        model=<span class="string">"gpt-4o-mini"</span>,
        messages=conversation
    )

    <span class="comment"># Get the AI's story text</span>
    story_text = response.choices[<span class="number">0</span>].message.content

    <span class="comment"># Save the response to conversation history</span>
    conversation.append({
        <span class="string">"role"</span>: <span class="string">"assistant"</span>,
        <span class="string">"content"</span>: story_text
    })

    <span class="comment"># Display the story</span>
    <span class="function">print</span>(story_text)
    <span class="function">print</span>()

    <span class="comment"># Ask what the player wants to do</span>
    choice = <span class="function">input</span>(<span class="string">"What do you choose? (1, 2, 3, or 'quit'): "</span>)

    <span class="comment"># If they type quit, end the game</span>
    <span class="keyword">if</span> choice.lower() == <span class="string">"quit"</span>:
        <span class="function">print</span>()
        <span class="function">print</span>(<span class="string">"Thanks for playing, "</span> + player_name + <span class="string">"!"</span>)
        <span class="function">print</span>(<span class="string">"May the Code be with you!"</span>)
        <span class="keyword">break</span>

    <span class="comment"># Send the player's choice to the AI</span>
    conversation.append({
        <span class="string">"role"</span>: <span class="string">"user"</span>,
        <span class="string">"content"</span>: <span class="string">"I choose option "</span> + choice
    })

    <span class="function">print</span>()
    <span class="function">print</span>(<span class="string">"Generating next chapter..."</span>)
    <span class="function">print</span>()</code></pre>

<div class="box box-purple">
  <div class="box-title">🟣 What Does This Code Do? (Section by Section)</div>
  <p><strong>while True:</strong> — Creates a loop that runs forever (until we stop it). Each cycle = one &#8220;turn&#8221; of the adventure.</p>
  <p><strong>client.chat.completions.create(&#8230;)</strong> — This is the line that actually calls ChatGPT! It sends the entire conversation and gets back a response.</p>
  <p><strong>response.choices[0].message.content</strong> — The AI&#8217;s response comes in a structured package. This pulls out just the story text.</p>
  <p><strong>conversation.append(&#8230;)</strong> — Saves the AI&#8217;s response to our conversation list so it remembers what happened.</p>
  <p><strong>input(&#8230;)</strong> — Pauses and waits for the player to type their choice.</p>
  <p><strong>if choice.lower() == &#8220;quit&#8221;:</strong> — Checks if the player typed &#8220;quit&#8221; (or &#8220;QUIT&#8221; or &#8220;Quit&#8221; — <code>.lower()</code> converts it to lowercase first).</p>
  <p><strong>break</strong> — Stops the loop and ends the game.</p>
</div>

<div class="box box-red">
  <div class="box-title">🔴 Watch the Indentation!</div>
  <p>Every line inside the <code>while True:</code> loop must start with <strong>4 spaces</strong>. Every line inside the <code>if</code> block must start with <strong>8 spaces</strong> (4 for the loop + 4 for the if).</p>
  <p>If you see <code>IndentationError</code>, check that every line has the right number of spaces. Thonny usually handles this automatically, but if you&#8217;ve been copying and pasting, the spacing might be off.</p>
</div>

<!-- STEP 12 -->
<div class="step-header"><span class="step-num">Step 12:</span><span class="step-title">Save and Run Your Program!</span></div>

<p>You&#8217;re done writing code! Time to test it.</p>
<ol>
  <li>Press <strong>Ctrl + S</strong> to save your file.</li>
  <li>Press the green <strong>Run ▶</strong> button at the top of Thonny (or press <strong>F5</strong>).</li>
  <li>Look at the bottom section of Thonny (the shell). You should see:</li>
</ol>

<div class="box box-teal">
<pre style="background: #E0F7FA !important; color: #333 !important; border: none !important; margin: 0 !important; padding: 0 !important;"><code style="color: #333 !important;">==================================================
  CHOOSE YOUR OWN ADVENTURE
  Powered by AI
==================================================

Enter your character's name: </code></pre>
</div>

<ol start="4">
  <li>Type a character name and press Enter. Wait a few seconds while ChatGPT generates the first chapter.</li>
  <li>Read the story! At the end, you&#8217;ll see 3 choices. Type <code>1</code>, <code>2</code>, or <code>3</code> and press Enter.</li>
  <li>When you&#8217;re done playing, type <code>quit</code> to end.</li>
</ol>

<div class="box box-orange">
  <div class="box-title">🟠 It Takes a Few Seconds!</div>
  <p>After you make a choice, the program sends your message to ChatGPT over the internet and waits for a response. This usually takes 3–10 seconds. Just be patient — the AI is writing your story!</p>
</div>

<!-- TROUBLESHOOTING -->
<h1>Troubleshooting</h1>
<p>If something goes wrong, check here first:</p>

<table>
  <thead><tr><th>Error</th><th>What to Do</th></tr></thead>
  <tbody>
    <tr><td><code>ModuleNotFoundError: No module named 'openai'</code></td><td>You skipped Step 2. Open the terminal and run: <code>pip install openai --break-system-packages</code></td></tr>
    <tr><td><code>AuthenticationError</code> or <code>Invalid API key</code></td><td>Your API key is wrong. Go back to <a href="https://www.sekol.ninja" target="_blank">www.sekol.ninja</a>, copy the key again, and make sure it&#8217;s pasted between the quotation marks. No extra spaces.</td></tr>
    <tr><td><code>IndentationError</code></td><td>Your spacing is wrong. Lines inside the while loop need 4 spaces. Lines inside the if block need 8 spaces. Delete the spaces and re-type them.</td></tr>
    <tr><td><code>SyntaxError</code></td><td>You have a typo. Check for missing quotation marks, missing colons (<code>:</code>) after <code>while True</code> and <code>if</code> statements, and missing parentheses.</td></tr>
    <tr><td>Program runs but nothing happens</td><td>It&#8217;s waiting for you to type! Look at the bottom of Thonny (the shell) for a blinking cursor.</td></tr>
    <tr><td><code>ConnectionError</code> or <code>Timeout</code></td><td>Your Pi isn&#8217;t connected to the internet. Check Wi-Fi (click the icon in the top-right).</td></tr>
    <tr><td><code>RateLimitError</code></td><td>Too many people using the key at once. Wait 30 seconds and try again.</td></tr>
  </tbody>
</table>

<!-- CUSTOMIZE -->
<h1>Make It Your Own!</h1>

<div class="box box-orange">
  <div class="box-title">🎨 Change the Story Theme</div>
  <p>Edit the system_message content (Step 6) to change what kind of stories the AI tells. Try:</p>
  <ul>
    <li>A fantasy quest with dragons and wizards</li>
    <li>A mystery detective story</li>
    <li>A pirate adventure on the high seas</li>
    <li>A zombie survival story (kid-friendly of course!)</li>
    <li>A time-travel adventure through history</li>
  </ul>
</div>

<div class="box box-orange">
  <div class="box-title">🚀 Add More Player Customization</div>
  <p>Ask the player more questions at the start:</p>
  <pre style="margin: 8px 0 !important;"><code>companion = <span class="function">input</span>(<span class="string">"Name your sidekick: "</span>)
superpower = <span class="function">input</span>(<span class="string">"What is your superpower? "</span>)</code></pre>
  <p>Then include them in the first_message content so the AI knows about them!</p>
</div>

<div class="box box-orange">
  <div class="box-title">🏆 Challenge: Add a Score System</div>
  <p>Create a variable called <code>score = 0</code> at the beginning. Modify the system message to tell the AI to include score changes in the story like &#8220;+10 points for bravery!&#8221; Then use Python to track and display the running score.</p>
</div>

<!-- COMPLETE CODE -->
<h1>Complete Code (All Together)</h1>

<p>Here is the entire program in one place. If you got lost during the steps, use this as a reference. Remember to replace <code>paste-your-api-key-here</code> with your real API key from <a href="https://www.sekol.ninja" target="_blank">www.sekol.ninja</a>.</p>

<pre><code><span class="comment"># ============================================</span>
<span class="comment"># CHOOSE YOUR OWN ADVENTURE</span>
<span class="comment"># Powered by AI (ChatGPT)</span>
<span class="comment"># ============================================</span>

<span class="comment"># Import the library we need</span>
<span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI

<span class="comment"># Set up the connection to ChatGPT</span>
<span class="comment"># Get your API key from www.sekol.ninja</span>
client = OpenAI(api_key=<span class="string">"paste-your-api-key-here"</span>)

<span class="comment"># Tell the AI what kind of stories to create</span>
system_message = {
    <span class="string">"role"</span>: <span class="string">"system"</span>,
    <span class="string">"content"</span>: <span class="string">"""You are an adventure story master for kids.
You create exciting choose-your-own-adventure stories.

RULES:
- Keep the story fun, exciting, and kid-friendly.
- Each response should be 2-3 short paragraphs.
- Always end with exactly 3 numbered choices for what to do next.
- Use vivid descriptions to make the story come alive.
- The story should have a sci-fi space adventure theme.
- Never include anything scary, violent, or inappropriate."""</span>
}

<span class="comment"># Create a list to keep track of the conversation</span>
conversation = [system_message]

<span class="comment"># Print the welcome screen</span>
<span class="function">print</span>(<span class="string">"="</span> * <span class="number">50</span>)
<span class="function">print</span>(<span class="string">"  CHOOSE YOUR OWN ADVENTURE"</span>)
<span class="function">print</span>(<span class="string">"  Powered by AI"</span>)
<span class="function">print</span>(<span class="string">"="</span> * <span class="number">50</span>)
<span class="function">print</span>()

<span class="comment"># Ask the player for their name</span>
player_name = <span class="function">input</span>(<span class="string">"Enter your character's name: "</span>)
<span class="function">print</span>()
<span class="function">print</span>(<span class="string">"Welcome, "</span> + player_name + <span class="string">"! Your adventure begins..."</span>)
<span class="function">print</span>()

<span class="comment"># Create the first message to start the story</span>
first_message = {
    <span class="string">"role"</span>: <span class="string">"user"</span>,
    <span class="string">"content"</span>: <span class="string">"Start a new adventure story. My character's name is "</span> + player_name + <span class="string">". Begin the story!"</span>
}
conversation.append(first_message)

<span class="comment"># The game loop</span>
<span class="keyword">while</span> True:
    <span class="comment"># Send the conversation to ChatGPT</span>
    response = client.chat.completions.create(
        model=<span class="string">"gpt-4o-mini"</span>,
        messages=conversation
    )

    <span class="comment"># Get the AI's story text</span>
    story_text = response.choices[<span class="number">0</span>].message.content

    <span class="comment"># Save the response to conversation history</span>
    conversation.append({
        <span class="string">"role"</span>: <span class="string">"assistant"</span>,
        <span class="string">"content"</span>: story_text
    })

    <span class="comment"># Display the story</span>
    <span class="function">print</span>(story_text)
    <span class="function">print</span>()

    <span class="comment"># Ask what the player wants to do</span>
    choice = <span class="function">input</span>(<span class="string">"What do you choose? (1, 2, 3, or 'quit'): "</span>)

    <span class="comment"># If they type quit, end the game</span>
    <span class="keyword">if</span> choice.lower() == <span class="string">"quit"</span>:
        <span class="function">print</span>()
        <span class="function">print</span>(<span class="string">"Thanks for playing, "</span> + player_name + <span class="string">"!"</span>)
        <span class="function">print</span>(<span class="string">"May the Code be with you!"</span>)
        <span class="keyword">break</span>

    <span class="comment"># Send the player's choice to the AI</span>
    conversation.append({
        <span class="string">"role"</span>: <span class="string">"user"</span>,
        <span class="string">"content"</span>: <span class="string">"I choose option "</span> + choice
    })

    <span class="function">print</span>()
    <span class="function">print</span>(<span class="string">"Generating next chapter..."</span>)
    <span class="function">print</span>()</code></pre>

<div class="hero-section" style="margin-top: 40px !important; padding: 25px !important;">
  <p style="font-size: 1.3em !important; margin: 0 !important;"><em>May the Code be with you!</em></p>
</div>

</div>
</body>
</html>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: sekol.ninja @ 2026-09-06 14:13:41 by W3 Total Cache
-->