{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
Content Copyright by Pierian Data
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Guessing Game Challenge - Solution\n", "\n", "Let's use `while` loops to create a guessing game.\n", "\n", "The Challenge:\n", "\n", "Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n", "\n", "1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n", "2. On a player's first turn, if their guess is\n", " * within 10 of the number, return \"WARM!\"\n", " * further than 10 away from the number, return \"COLD!\"\n", "3. On all subsequent turns, if a guess is \n", " * closer to the number than the previous guess return \"WARMER!\"\n", " * farther from the number than the previous guess, return \"COLDER!\"\n", "4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n", "\n", "Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import random\n", "\n", "num = random.randint(1,100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Next, print an introduction to the game and explain the rules" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WELCOME TO GUESS ME!\n", "I'm thinking of a number between 1 and 100\n", "If your guess is more than 10 away from my number, I'll tell you you're COLD\n", "If your guess is within 10 of my number, I'll tell you you're WARM\n", "If your guess is farther than your most recent guess, I'll say you're getting COLDER\n", "If your guess is closer than your most recent guess, I'll say you're getting WARMER\n", "LET'S PLAY!\n" ] } ], "source": [ "print(\"WELCOME TO GUESS ME!\")\n", "print(\"I'm thinking of a number between 1 and 100\")\n", "print(\"If your guess is more than 10 away from my number, I'll tell you you're COLD\")\n", "print(\"If your guess is within 10 of my number, I'll tell you you're WARM\")\n", "print(\"If your guess is farther than your most recent guess, I'll say you're getting COLDER\")\n", "print(\"If your guess is closer than your most recent guess, I'll say you're getting WARMER\")\n", "print(\"LET'S PLAY!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create a list to store guesses\n", "\n", "Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "guesses = [0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm thinking of a number between 1 and 100.\n", " What is your guess? 500\n", "OUT OF BOUNDS! Please try again: \n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 50\n" ] } ], "source": [ "while True:\n", " \n", " guess = int(input(\"I'm thinking of a number between 1 and 100.\\n What is your guess? \"))\n", " \n", " if guess < 1 or guess > 100:\n", " print('OUT OF BOUNDS! Please try again: ')\n", " continue\n", " \n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n", "\n", "Some hints:\n", "* it may help to sketch out all possible combinations on paper first!\n", "* you can use the `abs()` function to find the positive difference between two numbers\n", "* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm thinking of a number between 1 and 100.\n", " What is your guess? 50\n", "COLD!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 75\n", "WARMER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 85\n", "WARMER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 92\n", "COLDER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 80\n", "WARMER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 78\n", "COLDER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 82\n", "WARMER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 83\n", "COLDER!\n", "I'm thinking of a number between 1 and 100.\n", " What is your guess? 81\n", "CONGRATULATIONS, YOU GUESSED IT IN ONLY 9 GUESSES!!\n" ] } ], "source": [ "while True:\n", "\n", " # we can copy the code from above to take an input\n", " guess = int(input(\"I'm thinking of a number between 1 and 100.\\n What is your guess? \"))\n", " \n", " if guess < 1 or guess > 100:\n", " print('OUT OF BOUNDS! Please try again: ')\n", " continue\n", " \n", " # here we compare the player's guess to our number\n", " if guess == num:\n", " print(f'CONGRATULATIONS, YOU GUESSED IT IN ONLY {len(guesses)} GUESSES!!')\n", " break\n", " \n", " # if guess is incorrect, add guess to the list\n", " guesses.append(guess)\n", " \n", " # when testing the first guess, guesses[-2]==0, which evaluates to False\n", " # and brings us down to the second section\n", " \n", " if guesses[-2]: \n", " if abs(num-guess) < abs(num-guesses[-2]):\n", " print('WARMER!')\n", " else:\n", " print('COLDER!')\n", " \n", " else:\n", " if abs(num-guess) <= 10:\n", " print('WARM!')\n", " else:\n", " print('COLD!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's it! You've just programmed your first game!\n", "\n", "In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Good Job!" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }