{ "metadata": { "name": "ch2" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": "Chapter 2 : Truth, Branching, and the Game Loop: Guess My Number" }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 2.1 page no : 41" }, { "cell_type": "code", "collapsed": false, "input": "'''\nScore Rater\nDemonstrates the if statement\n'''\n\nif (True):\n print \"This is always displayed.\\n\\n\";\n\nif (False):\n print \"This is never displayed.\\n\\n\";\nscore = 1000;\nif (score):\n print \"At least you didn't score zero.\\n\\n\";\n\nif (score >= 250):\n print \"You scored 250 or more. Decent.\\n\\n\";\n\nif (score >= 500):\n print \"You scored 500 or more. Nice.\\n\\n\";\nif (score >= 1000):\n print \"You scored 1000 or more. Impressive!\\n\";", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "This is always displayed.\n\n\nAt least you didn't score zero.\n\n\nYou scored 250 or more. Decent.\n\n\nYou scored 500 or more. Nice.\n\n\nYou scored 1000 or more. Impressive!\n\n" } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 2.2 page no :46" }, { "cell_type": "code", "collapsed": false, "input": "'''\nScore Rater 2.0\nDemonstrates an else clause\n'''\n\nprint \"Enter your score: \";\nscore = 1500 #int(raw_input())\nif (score >= 1000):\n print \"You scored 1000 or more. Impressive!\\n\";\nelse:\n print \"You scored less than 1000.\\n\";", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter your score: \nYou scored 1000 or more. Impressive!\n\n" } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 2.3 page no : 49" }, { "cell_type": "code", "collapsed": false, "input": "'''\n// Score Rater 3.0\n// Demonstrates if else-if else suite\n'''\n\n\nprint \"Enter your score: \";\nscore = 1500 #int(raw_input())\n\nif (score >= 1000):\n print \"You scored 1000 or more. Impressive!\\n\";\nelif (score >= 500):\n print \"You scored 500 or more. Nice.\\n\";\nelif (score >= 250):\n print \"You scored 250 or more. Decent.\\n\";\nelse:\n print \"You scored less than 250. Nothing to brag about.\\n\";", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter your score: \nYou scored 1000 or more. Impressive!\n\n" } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.4 page no : 53" }, { "cell_type": "code", "collapsed": false, "input": "'''\n// Menu Chooser\n// Demonstrates the switch statement\n'''\n\nprint \"Difficulty Levels\\n\\n\";\nprint \"1 - Easy\\n\";\nprint \"2 - Normal\\n\";\nprint \"3 - Hard\\n\\n\";\n\nprint \"Choice: \";\nchoice = 2 #int(raw_input())\nif choice==1:\n print \"You picked Easy.\\n\";\nelif choice==2: \n print \"You picked Normal.\\n\";\nelif choice==3:\n \"You picked Hard.\\n\";\nelse: \n \"You made an illegal choice.\\n\";", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Difficulty Levels\n\n\n1 - Easy\n\n2 - Normal\n\n3 - Hard\n\n\nChoice: \nYou picked Normal.\n\n" } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.5 page no : 55" }, { "cell_type": "code", "collapsed": false, "input": "'''\nPlay Again\nDemonstrates while loops\n'''\nchoices = ['y','y','n']\nagain = 'y'\ni = 0\nwhile (again == 'y'):\n print \"\\n**Played an exciting game**\";\n print \"\\nDo you want to play again? (y/n): \",\n again = choices[i] #raw_input()\n i += 1\n\nprint \"\\nOkay, bye.\";", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "\n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \nOkay, bye.\n" } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.6 page no : 57" }, { "cell_type": "code", "collapsed": false, "input": "'''\nPlay Again\nDemonstrates while loops\n'''\nchoices = ['y','y','n']\ni = 0\nagain = 'y'\nwhile (again == 'y'):\n print \"\\n**Played an exciting game**\";\n print \"\\nDo you want to play again? (y/n): \",\n again = choices[i] #raw_input()\n i += 1\n\nprint \"\\nOkay, bye.\";", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "\n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \nOkay, bye.\n" } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.7 page no :59" }, { "cell_type": "code", "collapsed": false, "input": "'''\n// Finicky Counter\n// Demonstrates break and continue statements\n'''\ncount = 0;\nwhile (True):\n count += 1;\n #end loop if count is greater than 10\n if (count > 10):\n break;\n #skip the number 5\n if (count == 5):\n continue;\n print count", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "1\n2\n3\n4\n6\n7\n8\n9\n10\n" } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.8 page no : 63" }, { "cell_type": "code", "collapsed": false, "input": "'''\n// Designers Network\n// Demonstrates logical operators\n'''\n\nprint \"\\tGame Designer's Network\\n\";\nsuccess = False\nwhile not success:\n print \"\\nUsername: \";\n username = 'guest' #raw_input()\n print \"Password: \";\n password = 'guest' #raw_input()\n if (username == \"S.Meier\" and password == \"civilization\"):\n print \"\\nHey, Sid.\";\n success = True;\n elif (username == \"S.Miyamoto\" and password == \"mariobros\"):\n print \"\\nWhat's up, Shigeru?\";\n success = True;\n elif (username == \"W.Wright\" and password == \"thesims\"):\n print \"\\nHow goes it, Will?\";\n success = True;\n elif (username == \"guest\" or password == \"guest\"):\n print \"\\nWelcome, guest.\";\n success = True;\n else:\n print \"\\nYour login failed.\";\n success = False;", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "\tGame Designer's Network\n\n\nUsername: \nPassword: \n\nWelcome, guest.\n" } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.9 page no : 68" }, { "cell_type": "code", "collapsed": false, "input": "'''\n// Die Roller\n// Demonstrates generating random numbers\n'''\nimport random\n\nrandomNumber = random.random()*100;\n#seed random number generator\n#generate random number\ndie = int(randomNumber % 6) + 1; # get a number between 1 and 6\nprint \"You rolled a \" , die", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "You rolled a 4\n" } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "example 2.10 page no : 74" }, { "cell_type": "code", "collapsed": false, "input": "'''\nGuess My Number\nThe classic number guessing game\n'''\nimport random\nsecretNumber = int(random.random()*100) % 10 + 1;\ntries = 0;\n#seed random number generator\n# random number between 1 and 100\nprint \"\\tWelcome to Guess My Number\\n\\n\";\nguesses = [0,1,2,3,4,5,6,7,8,9,10]\ni = 0\nwhile True:\n print \"Enter a guess: \";\n guess = guesses[i] #int(raw_input())\n i += 1\n tries += 1\n if (guess > secretNumber):\n print \"Too high!\\n\\n\";\n elif (guess < secretNumber):\n print \"Too low!\\n\\n\";\n else:\n print \"\\nThat's it! You got it in \" , tries , \" guesses!\\n\";\n break", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "\tWelcome to Guess My Number\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \n\nThat's it! You got it in 7 guesses!\n\n" } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }