{"id":214919,"date":"2025-05-16T03:35:29","date_gmt":"2025-05-16T03:35:29","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=214919"},"modified":"2025-05-16T03:35:31","modified_gmt":"2025-05-16T03:35:31","slug":"chatbot-python-code-a-chatbot-is-a-computer-program-designed-to-emulate-human-conversation","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/05\/16\/chatbot-python-code-a-chatbot-is-a-computer-program-designed-to-emulate-human-conversation\/","title":{"rendered":"Chatbot (PYTHON CODE) A chatbot is a computer program designed to emulate human conversation."},"content":{"rendered":"\n<p> Chatbot (PYTHON CODE) A chatbot is a computer program designed to emulate human conversation. For this program you will use if statements, user input, and random numbers to create a basic chatbot. Here is the scenario: You have decided to start an online website. You are creating a prototype to show investors so you can raise money and launch your website. You should ask the user at least five questions and use if-elif-else statements and give answers depending on how they answer. Some responses should be based on what they type, and some should be based on random numbers. For example, if they say they are sad, your chatbot might respond \u201cI\u2019m sorry to hear that\u201d. You could also have a random number generated between 1 and 3 and have a corresponding response depending on the number such as \u201cThat is great to hear\u201d or \u201cSo interesting\u201d. Sample Run Please note: your program will be different &#8211; this is only an example (user input is in italics) What is your name? Pascal Hi there Pascal, nice to meet you. How old are you? 17 17 is a good age. You are old enough to drive. So, Pascal how are you today? happy You are happy. That is good to hear. Tell me more. I am still happy. Sounds interesting. Well, Pascal, it has been nice chatting with you. Use the &#8220;Run Code&#8221; button to interact with your code. When you are ready to submit your code for a score, click the &#8220;Grade&#8221; button.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here is the <strong>correct Python code<\/strong> for a basic chatbot that meets the project requirements, including asking at least five questions, using <code>if-elif-else<\/code> statements, and random number responses:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\n\n# Question 1: Ask for the user's name\nname = input(\"What is your name? \")\nprint(f\"Hi there {name}, nice to meet you.\")\n\n# Question 2: Ask the user's age\nage = int(input(\"How old are you? \"))\nif age &lt; 13:\n    print(\"You're quite young! Enjoy your childhood!\")\nelif 13 &lt;= age &lt; 20:\n    print(\"Ah, the teenage years. Lots of changes and discoveries.\")\nelse:\n    print(\"Great! Being an adult comes with many responsibilities.\")\n\n# Question 3: Ask how the user is feeling today\nfeeling = input(f\"So, {name}, how are you today? \").lower()\nif \"sad\" in feeling:\n    print(\"I'm sorry to hear that. I hope things get better soon.\")\nelif \"happy\" in feeling or \"good\" in feeling:\n    print(\"That's wonderful! Keep smiling.\")\nelse:\n    print(\"Thanks for sharing. Emotions are part of life.\")\n\n# Question 4: Ask what the user likes to do for fun\nhobby = input(\"What do you like to do for fun? \")\nprint(f\"{hobby.capitalize()} sounds like a lot of fun!\")\n\n# Question 5: Random response question\nprint(\"Let me think about something...\")\nresponse_num = random.randint(1, 3)\nif response_num == 1:\n    print(\"That's really fascinating!\")\nelif response_num == 2:\n    print(\"I haven\u2019t heard that in a while. Cool!\")\nelse:\n    print(\"That\u2019s interesting. You seem like a fun person.\")\n\n# Ending the chat\nprint(f\"Well, {name}, it has been nice chatting with you. Have a great day!\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Explanation (300+ words):<\/h3>\n\n\n\n<p>This Python chatbot program uses <strong>user input<\/strong>, <strong>conditional statements<\/strong>, and <strong>random number generation<\/strong> to simulate a simple yet interactive conversation between a chatbot and a user. It follows the structure required for a prototype demonstration, perfect for pitching to investors.<\/p>\n\n\n\n<p><strong>Step-by-step breakdown:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>User Input (Name):<\/strong><br>The program begins by asking for the user&#8217;s name using the <code>input()<\/code> function. The name is then used throughout the chat to personalize responses, making the conversation feel more natural and engaging.<\/li>\n\n\n\n<li><strong>Conditional Logic (Age):<\/strong><br>The chatbot then asks for the user&#8217;s age and uses an <code>if-elif-else<\/code> structure to respond appropriately based on the age range:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Under 13: Encourages youth to enjoy childhood.<\/li>\n\n\n\n<li>13\u201319: Acknowledges the teenage phase.<\/li>\n\n\n\n<li>20 and above: Emphasizes adult responsibilities.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Emotion Response:<\/strong><br>The chatbot asks how the user is feeling and checks the response for certain keywords using <code>.lower()<\/code> to ensure case insensitivity. The chatbot replies with empathetic or encouraging messages accordingly, demonstrating basic <strong>natural language processing<\/strong> logic.<\/li>\n\n\n\n<li><strong>Personal Interests:<\/strong><br>When the user shares a hobby, the chatbot reflects back using <code>capitalize()<\/code> to make the input look clean and to show acknowledgment, which reinforces user engagement.<\/li>\n\n\n\n<li><strong>Randomized Response:<\/strong><br>A random number between 1 and 3 is generated using <code>random.randint()<\/code>. Each number corresponds to a different unique response. This adds <strong>variation<\/strong> and <strong>surprise<\/strong>, making the chatbot less predictable and more enjoyable.<\/li>\n\n\n\n<li><strong>Closing the Chat:<\/strong><br>The chatbot wraps up the session warmly, again using the user&#8217;s name for personalization.<\/li>\n<\/ol>\n\n\n\n<p>Overall, this chatbot provides a basic yet functional conversation flow that demonstrates the ability to interact with users, understand input, and use conditional logic and randomness. This is an excellent foundation for more advanced chatbot features in the future.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chatbot (PYTHON CODE) A chatbot is a computer program designed to emulate human conversation. For this program you will use if statements, user input, and random numbers to create a basic chatbot. Here is the scenario: You have decided to start an online website. You are creating a prototype to show investors so you can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-214919","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/214919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=214919"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/214919\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=214919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=214919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=214919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}