{"id":39,"date":"2025-09-23T14:51:55","date_gmt":"2025-09-23T18:51:55","guid":{"rendered":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/?p=39"},"modified":"2025-11-08T17:45:30","modified_gmt":"2025-11-08T22:45:30","slug":"coding-assignment","status":"publish","type":"post","link":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/projects\/coding-assignment\/","title":{"rendered":"Coding Project &#8211; Akinator"},"content":{"rendered":"\n<p>For my coding assignment, I&#8217;ve created an Akinator like guessing game in Python, where the computer tries to figure out the character the user is thinking of. The program I created uses <br>AI to generate simple yes\/no questions, and the user responds until the AI makes a guess.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">Below is the flowchart I made for my program:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"433\" height=\"1024\" src=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-433x1024.png\" alt=\"\" class=\"wp-image-49\" style=\"width:645px;height:auto\" srcset=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-433x1024.png 433w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-127x300.png 127w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-768x1818.png 768w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-649x1536.png 649w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-865x2048.png 865w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Untitled-Diagram-Mermaid-Chart-Sept-21-2025-4-scaled.png 1081w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/figure>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\">Flowchart<\/p>\n\n\n\n<p>Image URL (clearer version): <a href=\"https:\/\/i.imgur.com\/ddYnl0R.png\">https:\/\/i.imgur.com\/ddYnl0R.png<\/a><\/p>\n\n\n\n<p>Source Code: <a href=\"https:\/\/replit.com\/@michaelhu28\/Python?v=1\">https:\/\/replit.com\/@michaelhu28\/Python?v=1<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Variables<\/h2>\n\n\n\n<pre class=\"wp-block-code has-accent-6-background-color has-background\" style=\"border-width:1px\"><code>history = \"\"      # stores past questions and answers\ngame_over = False # tracks if the game has ended\nquestion_count = 0 # counts number of questions asked<\/code><\/pre>\n\n\n\n<p>This is part of the variables I&#8217;ve defined to keep track of the game&#8217;s progress. The <code>history<\/code> is a. string which stores a log of the entire conversation. <strong><code>game_over<\/code><\/strong> is a boolean (meaning can be assigned True of False) that stores the status of the game. And <strong><code>question_count<\/code><\/strong> keeps a total of how many questions the AI has asked.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Functions<\/h2>\n\n\n\n<pre class=\"wp-block-code has-accent-6-background-color has-background\" style=\"border-width:2px\"><code>def get_user_answer():\n  #Gets user input\n  while True:\n    answer = input(\"You: \").strip().lower()\n    if answer in &#091;\"yes\", \"y\",\"no\",\"n\",\"maybe\",\"m\",\"stop\",\"quit\",\"q\"]:\n      if answer in &#091;\"yes\", \"y\"]:\n        return \"yes\"\n      elif answer in &#091;\"no\", \"n\"]:\n        return \"no\"\n      elif answer in &#091;\"maybe\", \"m\"]:\n        return \"maybe\"\n      elif answer in &#091;\"stop\", \"quit\", \"q\"]:\n        return \"stop\"\n      else:\n        print(\"Please answer with yes, no, maybe, or quit.\")<\/code><\/pre>\n\n\n\n<p>This function validates the user&#8217;s input. Instead of crashing or accepting a random input, this function keeps asking the user until they provide a valid answer. It also takes into account different forms of input (like &#8220;y&#8221; and &#8220;yes&#8221;) into consistent outputs for the AI.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Conditional Statements:<\/h2>\n\n\n\n<pre class=\"wp-block-code has-accent-6-background-color has-background\" style=\"border-width:2px\"><code>if ai_output&#091;\"type\"] == \"guess\":\n    ask = input(\"Is this correct? (yes\/no): \").strip().lower()\n    if ask in &#091;\"yes\", \"y\"]:\n        print(\"Great! I guessed it right.\")\n        print(f\"It took me {question_count} questions to guess it.\")\n        game_over = True\n    else:\n        print(\"Sorry, I couldn't guess it right. Let's try again.\")\n        history += f\"\\nAI: {ai_output}\\nYou: no (incorrect guess)\"\n        question_count += 1<\/code><\/pre>\n\n\n\n<p>This portion of the code checks if the AI is making a guess or asking a question. This is made possible by prompting the AI to respond in a JSON format. If the type of response is a guess and is correct, it ends the game with a success message. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Example response format:<\/p>\n\n\n\n<pre class=\"wp-block-code has-accent-6-background-color has-background\" style=\"border-width:2px\"><code>{ \"type\": \"question\", \"message\": \"Is your person real, not fictional?\", \n\n\"confidence\": 0.01, \n\n\"state\": { \"q_count\": 1, \"top_candidates\": &#091;\"Barack Obama\",\"Harry Potter\",\"Elvis Presley\"], \n\n\"notes\": \"Starting with real vs fictional split for major info gain.\" } \n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prompt Engineering:<\/h2>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"590\" data-id=\"53\" src=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.34-1024x590.png\" alt=\"\" class=\"wp-image-53\" srcset=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.34-1024x590.png 1024w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.34-300x173.png 300w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.34-768x442.png 768w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.34-1536x884.png 1536w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.34-2048x1179.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"671\" data-id=\"54\" src=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.41-1024x671.png\" alt=\"\" class=\"wp-image-54\" srcset=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.41-1024x671.png 1024w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.41-300x197.png 300w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.41-768x503.png 768w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.41-1536x1006.png 1536w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.40.41.png 2036w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/figure>\n\n\n\n<p class=\"has-medium-font-size\">I have explored with the prompt and the different models to let the AI guess correctly using the least amount of questions.  (left is the new prompt, right is the old one)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Pros and Cons of the New Prompt<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Pros<\/strong><\/td><td><strong>Cons<\/strong><\/td><\/tr><tr><td>Improves the accuracy of the response.<\/td><td>A longer prompt would result in longer response times.<\/td><\/tr><tr><td>Specifies the structure of the response, making it readable by the program.<\/td><td>Generating structured JSON with all that information can increase the token load.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-x-large-font-size\">Result:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"761\" data-id=\"56\" src=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.44.30-1024x761.png\" alt=\"\" class=\"wp-image-56\" srcset=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.44.30-1024x761.png 1024w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.44.30-300x223.png 300w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.44.30-768x571.png 768w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.44.30-1536x1142.png 1536w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.44.30.png 1614w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"928\" data-id=\"55\" src=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.41.49-1024x928.png\" alt=\"\" class=\"wp-image-55\" srcset=\"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.41.49-1024x928.png 1024w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.41.49-300x272.png 300w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.41.49-768x696.png 768w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.41.49-1536x1391.png 1536w, https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-content\/uploads\/sites\/33\/2025\/09\/Screenshot-2025-09-22-at-21.41.49.png 1826w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/figure>\n\n\n\n<p class=\"has-medium-font-size\">When thinking of the same character(Mr. Beast), by using the new prompt, the program is able to guess it correctly using less amount of questions (15 questions &#8211; 22 questions). The new prompt also shows a confidence score when asking the question.<\/p>\n\n\n\n<p class=\"has-small-font-size\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What I Learned<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>How to use flowcharting to plan my logic before coding.<\/li>\n\n\n\n<li>How to connect Python with the OpenAI API and structure prompts.<\/li>\n\n\n\n<li>How to use loops, conditionals, and functions to make code readable and organized.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Future Improvements<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a <strong>confidence score<\/strong> so the AI can explain how sure it is about a guess. (Added)<\/li>\n\n\n\n<li>Save the <strong>transcript<\/strong> of each game to a file so I can analyze failures.<\/li>\n\n\n\n<li>Build a<strong> version with GUI<\/strong> <\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Update (Sep. 28):<\/h2>\n\n\n\n<p>After receiving feedback that my original setup required a paid API and wasn\u2019t directly runnable, I updated my project to use the Google Gemini API. This change allows anyone to clone the project and run the program for free without extra setup. To make the project accessible over time, I also recorded a short walk-through video demonstrating how the game works in action. This way, even if the API service changes or keys expire, readers can still clearly see the program\u2019s functionality.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Akinator Blog Post Update\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/NHduJl8PQtA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-a89b3969 wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/chatgpt.com\/share\/68d0c050-4ffc-800b-9454-253ad66fc2f2\">Chat History With AI<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center has-x-large-font-size\">Thanks for Reading my First Blog Post!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my coding assignment, I&#8217;ve created an Akinator like guessing game in Python, where the computer tries to figure out the character the user is thinking of. The program I created uses AI to generate simple yes\/no questions, and the user responds until the AI makes a guess. Below is the flowchart I made for [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-intro","category-projects"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":14,"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/posts\/39\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.stgeorges.bc.ca\/michaelh\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}