{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Guessing Game" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A guessing game where the Computer thinks of a random number between 1 and 100 and the User tries to guess the number!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "I am thinking of a number...\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 2\n", "What is your guess? 3\n", "What is your guess? 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "That's right!\n" ] } ], "source": [ "secret = random.randint(1,100)\n", "print(secret) # we will delete this statement after we have completed coding this game\n", "print(\"I am thinking of a number...\")\n", "while True:\n", " guess = int(input(\"What is your guess? \"))\n", " if guess == secret:\n", " print(\"That's right!\")\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The game is not interesting unless the computer gives the user a hint!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65\n", "I am thinking of a number...\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? a23\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'a23'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"I am thinking of a number...\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mguess\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"What is your guess? \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mguess\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0msecret\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"That's right!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a23'" ] } ], "source": [ "secret = random.randint(0,100)\n", "print(secret)\n", "print(\"I am thinking of a number...\")\n", "while True:\n", " guess = int(input(\"What is your guess? \"))\n", " if guess == secret:\n", " print(\"That's right!\")\n", " break\n", " elif guess < secret:\n", " print(\"My number is bigger\")\n", " else:\n", " print(\"My number is smaller\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "83\n", "I am thinking of a number...\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? a23\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Please input a number.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 23\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "My number is bigger\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 83\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "That's right!\n" ] } ], "source": [ "secret = random.randint(0,100)\n", "print(secret)\n", "print(\"I am thinking of a number...\")\n", "while True:\n", " try:\n", " guess = int(input(\"What is your guess? \"))\n", " except ValueError:\n", " print(\"Please input a number.\")\n", " continue\n", " if guess == secret:\n", " print(\"That's right!\")\n", " break\n", " elif guess < secret:\n", " print(\"My number is bigger\")\n", " else:\n", " print(\"My number is smaller\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "what if user gives a number outside the range, 1..100?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "I am thinking of a number...\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 9\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "My number is smaller\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? -23\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Please input a number between 1 and 100\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 101\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Please input a number between 1 and 100\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 6\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "That's right!\n" ] } ], "source": [ "secret = random.randint(0,100)\n", "print(secret)\n", "print(\"I am thinking of a number...\")\n", "while True:\n", " try:\n", " guess = int(input(\"What is your guess? \"))\n", " except ValueError:\n", " print(\"Please input a number.\")\n", " continue\n", " if guess < 1 or guess > 100:\n", " print(\"Please input a number between {} and {}\".format(1,100))\n", " continue\n", " if guess == secret:\n", " print(\"That's right!\")\n", " break\n", " elif guess < secret:\n", " print(\"My number is bigger\")\n", " else:\n", " print(\"My number is smaller\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we want to change the range in this game?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "minguess = 1\n", "maxguess = 100\n", "secret = random.randint(minguess,maxguess)\n", "print(secret)\n", "print(\"I am thinking of a number...\")\n", "while True:\n", " try:\n", " guess = int(input(\"What is your guess? \"))\n", " except ValueError:\n", " print(\"Please input a number.\")\n", " continue\n", " if guess < minguess or guess > maxguess:\n", " print(\"Please input a number between {} and {}\".format(minguess,maxguess))\n", " continue\n", " if guess == secret:\n", " print(\"That's right!\")\n", " break\n", " elif guess < secret:\n", " print(\"My number is bigger\")\n", " else:\n", " print(\"My number is smaller\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Let us keep track of number of guesses" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "82\n", "I am thinking of a number...\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 22\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "My number is bigger\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 33\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "My number is bigger\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 44\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "My number is bigger\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "What is your guess? 82\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "That's right!\n", "You needed 4 guesses\n" ] } ], "source": [ "### minguess = 1\n", "maxguess = 100\n", "secret = random.randint(minguess,maxguess)\n", "print(secret)\n", "print(\"I am thinking of a number...\")\n", "num_guesses = 0\n", "while True:\n", " try:\n", " guess = int(input(\"What is your guess? \"))\n", " except ValueError:\n", " print(\"Please input a number.\")\n", " continue\n", " if guess < minguess or guess > maxguess:\n", " print(\"Please input a number between {} and {}\".format(minguess,maxguess))\n", " continue\n", " num_guesses = num_guesses + 1\n", " if guess == secret:\n", " print(\"That's right!\")\n", " break\n", " elif guess < secret:\n", " print(\"My number is bigger\")\n", " else:\n", " print(\"My number is smaller\")\n", "print(\"You needed {} guesses\".format(num_guesses))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.16" } }, "nbformat": 4, "nbformat_minor": 4 }