{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4 : The Standard Template Library: Hangman" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 4.1 page no : 118" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "// Hero's Inventory 2.0\n", "// Demonstrates vectors\n", "'''\n", "\n", "inventory = []\n", "inventory.append(\"sword\");\n", "inventory.append(\"armor\");\n", "inventory.append(\"shield\");\n", "print \"You have \" , len(inventory) , \" items.\";\n", "print \"\\nYour items:\";\n", "for i in inventory:\n", " print i\n", "\n", "print \"\\nYou trade your sword for a battle axe.\";\n", "inventory[0] = \"battle axe\";\n", "print \"\\nYour items:\\n\";\n", "for i in inventory:\n", " print i\n", "\n", "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", "print len(inventory[0]) , \" letters in it.\";\n", "print \"\\nYour shield is destroyed in a fierce battle.\";\n", "inventory.pop();\n", "print \"\\nYour items:\";\n", "for i in inventory:\n", " print i\n", "\n", "print \"\\nYou were robbed of all of your possessions by a thief.\";\n", "inventory = []\n", "if (not inventory):\n", " print \"\\nYou have nothing.\";\n", "else:\n", " print \"\\nYou have at least one item.\\n\";" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You have 3 items.\n", "\n", "Your items:\n", "sword\n", "armor\n", "shield\n", "\n", "You trade your sword for a battle axe.\n", "\n", "Your items:\n", "\n", "battle axe\n", "armor\n", "shield\n", "\n", "The item name ' battle axe ' has 10 letters in it.\n", "\n", "Your shield is destroyed in a fierce battle.\n", "\n", "Your items:\n", "battle axe\n", "armor\n", "\n", "You were robbed of all of your possessions by a thief.\n", "\n", "You have nothing.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 4.2 page no : 124" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "// Hero's Inventory 3.0\n", "// Demonstrates iterators\n", "'''\n", "\n", "inventory = []\n", "inventory.append(\"sword\");\n", "inventory.append(\"armor\");\n", "inventory.append(\"shield\");\n", "print \"You have \" , len(inventory) , \" items.\";\n", "print \"\\nYour items:\";\n", "for i in inventory:\n", " print i\n", "\n", "print \"\\nYou trade your sword for a battle axe.\";\n", "inventory[0] = \"battle axe\";\n", "print \"\\nYour items:\\n\";\n", "for i in inventory:\n", " print i\n", "\n", "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", "print len(inventory[0]) , \" letters in it.\";\n", "\n", "print \"You recover a crossbow from a slain enemy.\";\n", "inventory.insert(0,\"crossbow\");\n", "print \"Your items:\";\n", "for i in inventory:\n", " print i\n", " \n", "print \"\\nYour shield is destroyed in a fierce battle.\";\n", "inventory.pop();\n", "print \"\\nYour items:\";\n", "for i in inventory:\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You have 3 items.\n", "\n", "Your items:\n", "sword\n", "armor\n", "shield\n", "\n", "You trade your sword for a battle axe.\n", "\n", "Your items:\n", "\n", "battle axe\n", "armor\n", "shield\n", "\n", "The item name ' battle axe ' has 10 letters in it.\n", "You recover a crossbow from a slain enemy.\n", "Your items:\n", "crossbow\n", "battle axe\n", "armor\n", "shield\n", "\n", "Your shield is destroyed in a fierce battle.\n", "\n", "Your items:\n", "crossbow\n", "battle axe\n", "armor\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 4.3 page no : 132" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "// High Scores\n", "// Demonstrates algorithms\n", "'''\n", "print \"Creating a list of scores.\";\n", "scores = []\n", "scores.append(1500);\n", "scores.append(3500);\n", "scores.append(7500);\n", "print \"\\nHigh Scores:\";\n", "for i in scores:\n", " print i\n", "\n", "print \"\\nFinding a score.\";\n", "\n", "print \"\\nEnter a score to find: \";\n", "score = 3500 #int(raw_input())\n", "iter = score in scores\n", "if (iter):\n", " print \"Score found.\";\n", "else:\n", " print \"Score not found.\";\n", "\n", "print \"\\nRandomizing scores.\";\n", "import random\n", "for i in range(len(scores)):\n", " scores[i] = int(random.random() * 10000) \n", "\n", "print \"\\nHigh Scores:\\n\";\n", "for i in scores:\n", " print i\n", " \n", "print \"\\nSorting scores.\";\n", "scores.sort()\n", "print \"\\nHigh Scores:\\n\";\n", "for i in scores:\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Creating a list of scores.\n", "\n", "High Scores:\n", "1500\n", "3500\n", "7500\n", "\n", "Finding a score.\n", "\n", "Enter a score to find: \n", "Score found.\n", "\n", "Randomizing scores.\n", "\n", "High Scores:\n", "\n", "6658\n", "5396\n", "8522\n", "\n", "Sorting scores.\n", "\n", "High Scores:\n", "\n", "5396\n", "6658\n", "8522\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 4.4 page no: 142" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "// Hangman\n", "// The classic game of hangman\n", "'''\n", "#setup\n", "MAX_WRONG = 8;\n", "#maximum number of incorrect guesses allowed\n", "words = [] # collection of possible words to guess\n", "words.append(\"GUESS\");\n", "words.append(\"HANGMAN\");\n", "words.append(\"DIFFICULT\");\n", "THE_WORD = words[0];\n", "wrong = 0;\n", "soFar = ''\n", "for i in range(len(THE_WORD)):\n", " soFar += '-'\n", "\n", "used = \"\";\n", "\n", "print \"Welcome to Hangman. Good luck!\\n\";\n", "# main loop\n", "while ((wrong < MAX_WRONG) and (soFar != THE_WORD)):\n", " print \"\\n\\nYou have \" , (MAX_WRONG - wrong);\n", " print \" incorrect guesses left.\";\n", " print \"\\nYou've used the following letters:\\n\" , used \n", " print \"\\nSo far, the word is:\" , soFar\n", " print \"\\nEnter your guess: \";\n", " guess = raw_input()\n", " guess = guess.upper() #make uppercase since secret word in uppercase\n", " while guess in used:\n", " print \"\\nYou've already guessed \" , guess \n", " print \"Enter your guess: \";\n", " guess = 'GUESS' #raw_input()\n", " guess = guess.upper()\n", " used += guess;\n", " soFar = ''\n", " if (guess in THE_WORD):\n", " print \"That's right! \" , guess , \" is in the word.\";\n", " #update soFar to include newly guessed letter\n", " for i in range(len(THE_WORD)):\n", " if (THE_WORD[i] == guess):\n", " soFar += guess\n", " else:\n", " print \"Sorry, \" , guess , \" isn't in the word.\";\n", " wrong += 1\n", " #shut down\n", " if (wrong == MAX_WRONG):\n", " print \"\\nYou've been hanged!\";\n", " else:\n", " print \"\\nYou guessed it!\";\n", " print \"\\nThe word was \" , THE_WORD " ], "language": "python", "metadata": {}, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support stdin.", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 27\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nSo far, the word is:\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0msoFar\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nEnter your guess: \"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 29\u001b[1;33m \u001b[0mguess\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 30\u001b[0m \u001b[0mguess\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mguess\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#make uppercase since secret word in uppercase\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mguess\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mused\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 343\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mident\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 344\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 345\u001b[1;33m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_no_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 346\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 347\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpy3compat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPY3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m_no_raw_input\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 686\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 687\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 688\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 689\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 690\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support stdin." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Welcome to Hangman. Good luck!\n", "\n", "\n", "\n", "You have 8\n", " incorrect guesses left.\n", "\n", "You've used the following letters:\n", "\n", "\n", "So far, the word is: -----\n", "\n", "Enter your guess: \n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }