diff options
Diffstat (limited to 'C++_from_the_Ground/Chapter_4(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_4(1).ipynb | 1063 |
1 files changed, 1004 insertions, 59 deletions
diff --git a/C++_from_the_Ground/Chapter_4(1).ipynb b/C++_from_the_Ground/Chapter_4(1).ipynb index 5bc3dfe7..ebd72abc 100644 --- a/C++_from_the_Ground/Chapter_4(1).ipynb +++ b/C++_from_the_Ground/Chapter_4(1).ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 4" + "name": "", + "signature": "sha256:7a7e07f5e71ec203dfa93cc13b4ce9de80c80af3b59f8d7d6bebe41c247db83e" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,17 +11,36 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h1>Chapter 4: Program Control Statements<h1>" + "source": [ + "<h1>Chapter 4: Program Control Statements<h1>" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.1, Page Number: 58<h3>" + "source": [ + "<h3>Example 4.1, Page Number: 58<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Magic Number Program'''\n\nimport random\n\n#Variable declaration and initialization\nmagic = random.randint(0,100) #Number which the user has to guess\nguess = 10 #Number which the user guesses\n\nif guess == magic:\n print \"***Right***\" #Result\n\n\n \n\n", + "input": [ + "\n", + "\n", + "import random\n", + "\n", + "#Variable declaration and initialization\n", + "magic = random.randint(0,100) #Number which the user has to guess\n", + "guess = 10 #Number which the user guesses\n", + "\n", + "if guess == magic:\n", + " print \"***Right***\" #Result\n", + "\n", + "\n", + " \n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [], @@ -29,19 +49,43 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.2, Page Number: 59<h3>" + "source": [ + "<h3>Example 4.2, Page Number: 59<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Magic Number program: 1st improvement'''\n\nimport random\n\n#Variable decleration and initialization\nmagic=random.randint(0,100) #Number to be guessed\nguess = 10 #Number the user guesses\n\n#Result\nif guess == magic:\n print \"***Right***\"\nelse:\n print \"... Sorry, you're wrong.\"\n\n\n\n \n\n", + "input": [ + "\n", + "\n", + "\n", + "import random\n", + "\n", + "#Variable decleration and initialization\n", + "magic=random.randint(0,100) #Number to be guessed\n", + "guess = 10 #Number the user guesses\n", + "\n", + "#Result\n", + "if guess == magic:\n", + " print \"***Right***\"\n", + "else:\n", + " print \"... Sorry, you're wrong.\"\n", + "\n", + "\n", + "\n", + " \n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "... Sorry, you're wrong.\n" + "text": [ + "... Sorry, you're wrong.\n" + ] } ], "prompt_number": 8 @@ -49,19 +93,37 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.3, Page Number: 60<h3>" + "source": [ + "<h3>Example 4.3, Page Number: 60<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''#Divide the first number by the second.'''\n\n#Variable decleration and initialization\na = 30 #User input for the two nos.\nb = 10\n\n#Calculation and Result\nif b:\n print a/b\nelse:\n print \"Cannot divide by zero.\"\n\n\n", + "input": [ + "\n", + "\n", + "#Variable decleration and initialization\n", + "a = 30 #User input for the two nos.\n", + "b = 10\n", + "\n", + "#Calculation and Result\n", + "if b:\n", + " print a/b\n", + "else:\n", + " print \"Cannot divide by zero.\"\n", + "\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "3\n" + "text": [ + "3\n" + ] } ], "prompt_number": 11 @@ -69,12 +131,40 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.4, Page Number: 61<h3>" + "source": [ + "<h3>Example 4.4, Page Number: 61<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Magic Number Program: 2nd improvement'''\n\nimport random\n\n#Variable decleration \nmagic = random.randint(0,100) #Numbr to be guessed\nguess = 10 #Number the user guesses\n\n#Result\nif guess == magic:\n print \"***Right***\"\n print magic,\" is the magic number.\"\nelse:\n print \"... Sorry, you're wrong\" \n if(guess>magic): #use a nested if statement\n print \"Your guess is too high\"\n else:\n print \"Your guess is too low\"\n\n \n \n\n \n \n", + "input": [ + "\n", + "\n", + "\n", + "import random\n", + "\n", + "#Variable decleration \n", + "magic = random.randint(0,100) #Numbr to be guessed\n", + "guess = 10 #Number the user guesses\n", + "\n", + "#Result\n", + "if guess == magic:\n", + " print \"***Right***\"\n", + " print magic,\" is the magic number.\"\n", + "else:\n", + " print \"... Sorry, you're wrong\" \n", + " if(guess>magic): #use a nested if statement\n", + " print \"Your guess is too high\"\n", + " else:\n", + " print \"Your guess is too low\"\n", + "\n", + " \n", + " \n", + "\n", + " \n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [] @@ -82,19 +172,46 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.5, Page Number: 62<h3>" + "source": [ + "<h3>Example 4.5, Page Number: 62<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Demonstrate an if-else-if ladder'''\n\nfor x in range(6):\n if x==1: #using if-else ladder in a for loop\n print \"x is one\"\n elif x==2:\n print \"x is two\"\n elif x==3:\n print \"x is three\"\n elif x==4:\n print \"x is four\"\n else:\n print \"x is not between 1 nd 4\"\n \n \n \n", + "input": [ + "\n", + "\n", + "\n", + "for x in range(6):\n", + " if x==1: #using if-else ladder in a for loop\n", + " print \"x is one\"\n", + " elif x==2:\n", + " print \"x is two\"\n", + " elif x==3:\n", + " print \"x is three\"\n", + " elif x==4:\n", + " print \"x is four\"\n", + " else:\n", + " print \"x is not between 1 nd 4\"\n", + " \n", + " \n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "x is not between 1 nd 4\nx is one\nx is two\nx is three\nx is four\nx is not between 1 nd 4\n" + "text": [ + "x is not between 1 nd 4\n", + "x is one\n", + "x is two\n", + "x is three\n", + "x is four\n", + "x is not between 1 nd 4\n" + ] } ], "prompt_number": 13 @@ -102,19 +219,131 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.6, Page Number: 63<h3>" + "source": [ + "<h3>Example 4.6, Page Number: 63<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''For loop demonstration'''\n\nimport math\n\nfor num in range(100):\n sq_root = math.sqrt(float(num)) #Calculation\n print num,\" \",sq_root #Result\n ", + "input": [ + "\n", + "\n", + "import math\n", + "\n", + "for num in range(100):\n", + " sq_root = math.sqrt(float(num)) #Calculation\n", + " print num,\" \",sq_root #Result\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "0 0.0\n1 1.0\n2 1.41421356237\n3 1.73205080757\n4 2.0\n5 2.2360679775\n6 2.44948974278\n7 2.64575131106\n8 2.82842712475\n9 3.0\n10 3.16227766017\n11 3.31662479036\n12 3.46410161514\n13 3.60555127546\n14 3.74165738677\n15 3.87298334621\n16 4.0\n17 4.12310562562\n18 4.24264068712\n19 4.35889894354\n20 4.472135955\n21 4.58257569496\n22 4.69041575982\n23 4.79583152331\n24 4.89897948557\n25 5.0\n26 5.09901951359\n27 5.19615242271\n28 5.29150262213\n29 5.38516480713\n30 5.47722557505\n31 5.56776436283\n32 5.65685424949\n33 5.74456264654\n34 5.83095189485\n35 5.9160797831\n36 6.0\n37 6.0827625303\n38 6.16441400297\n39 6.2449979984\n40 6.32455532034\n41 6.40312423743\n42 6.48074069841\n43 6.5574385243\n44 6.63324958071\n45 6.7082039325\n46 6.78232998313\n47 6.8556546004\n48 6.92820323028\n49 7.0\n50 7.07106781187\n51 7.14142842854\n52 7.21110255093\n53 7.28010988928\n54 7.34846922835\n55 7.4161984871\n56 7.48331477355\n57 7.54983443527\n58 7.61577310586\n59 7.68114574787\n60 7.74596669241\n61 7.81024967591\n62 7.87400787401\n63 7.93725393319\n64 8.0\n65 8.0622577483\n66 8.12403840464\n67 8.18535277187\n68 8.24621125124\n69 8.30662386292\n70 8.36660026534\n71 8.42614977318\n72 8.48528137424\n73 8.54400374532\n74 8.60232526704\n75 8.66025403784\n76 8.71779788708\n77 8.77496438739\n78 8.83176086633\n79 8.88819441732\n80 8.94427191\n81 9.0\n82 9.05538513814\n83 9.11043357914\n84 9.16515138991\n85 9.21954445729\n86 9.2736184955\n87 9.32737905309\n88 9.38083151965\n89 9.43398113206\n90 9.48683298051\n91 9.53939201417\n92 9.59166304663\n93 9.64365076099\n94 9.69535971483\n95 9.74679434481\n96 9.79795897113\n97 9.8488578018\n98 9.89949493661\n99 9.94987437107\n" + "text": [ + "0 0.0\n", + "1 1.0\n", + "2 1.41421356237\n", + "3 1.73205080757\n", + "4 2.0\n", + "5 2.2360679775\n", + "6 2.44948974278\n", + "7 2.64575131106\n", + "8 2.82842712475\n", + "9 3.0\n", + "10 3.16227766017\n", + "11 3.31662479036\n", + "12 3.46410161514\n", + "13 3.60555127546\n", + "14 3.74165738677\n", + "15 3.87298334621\n", + "16 4.0\n", + "17 4.12310562562\n", + "18 4.24264068712\n", + "19 4.35889894354\n", + "20 4.472135955\n", + "21 4.58257569496\n", + "22 4.69041575982\n", + "23 4.79583152331\n", + "24 4.89897948557\n", + "25 5.0\n", + "26 5.09901951359\n", + "27 5.19615242271\n", + "28 5.29150262213\n", + "29 5.38516480713\n", + "30 5.47722557505\n", + "31 5.56776436283\n", + "32 5.65685424949\n", + "33 5.74456264654\n", + "34 5.83095189485\n", + "35 5.9160797831\n", + "36 6.0\n", + "37 6.0827625303\n", + "38 6.16441400297\n", + "39 6.2449979984\n", + "40 6.32455532034\n", + "41 6.40312423743\n", + "42 6.48074069841\n", + "43 6.5574385243\n", + "44 6.63324958071\n", + "45 6.7082039325\n", + "46 6.78232998313\n", + "47 6.8556546004\n", + "48 6.92820323028\n", + "49 7.0\n", + "50 7.07106781187\n", + "51 7.14142842854\n", + "52 7.21110255093\n", + "53 7.28010988928\n", + "54 7.34846922835\n", + "55 7.4161984871\n", + "56 7.48331477355\n", + "57 7.54983443527\n", + "58 7.61577310586\n", + "59 7.68114574787\n", + "60 7.74596669241\n", + "61 7.81024967591\n", + "62 7.87400787401\n", + "63 7.93725393319\n", + "64 8.0\n", + "65 8.0622577483\n", + "66 8.12403840464\n", + "67 8.18535277187\n", + "68 8.24621125124\n", + "69 8.30662386292\n", + "70 8.36660026534\n", + "71 8.42614977318\n", + "72 8.48528137424\n", + "73 8.54400374532\n", + "74 8.60232526704\n", + "75 8.66025403784\n", + "76 8.71779788708\n", + "77 8.77496438739\n", + "78 8.83176086633\n", + "79 8.88819441732\n", + "80 8.94427191\n", + "81 9.0\n", + "82 9.05538513814\n", + "83 9.11043357914\n", + "84 9.16515138991\n", + "85 9.21954445729\n", + "86 9.2736184955\n", + "87 9.32737905309\n", + "88 9.38083151965\n", + "89 9.43398113206\n", + "90 9.48683298051\n", + "91 9.53939201417\n", + "92 9.59166304663\n", + "93 9.64365076099\n", + "94 9.69535971483\n", + "95 9.74679434481\n", + "96 9.79795897113\n", + "97 9.8488578018\n", + "98 9.89949493661\n", + "99 9.94987437107\n" + ] } ], "prompt_number": 14 @@ -122,19 +351,29 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.7, Page Number: 64<h3>" + "source": [ + "<h3>Example 4.7, Page Number: 64<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Printin numbers 100 to -100 in decrements of 5'''\n\nfor i in xrange(100,-100,-5): \n print i,\n \n", + "input": [ + "\n", + "\n", + "for i in xrange(100,-100,-5): \n", + " print i,\n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45 -50 -55 -60 -65 -70 -75 -80 -85 -90 -95\n" + "text": [ + "100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45 -50 -55 -60 -65 -70 -75 -80 -85 -90 -95\n" + ] } ], "prompt_number": 1 @@ -142,19 +381,35 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.8, Page Number: 66<h3>" + "source": [ + "<h3>Example 4.8, Page Number: 66<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Runs a loop till 123 is typed'''\n\n#Variable decleration\nx=0\n\n#Loop till 123 is entered\nwhile x!=123:\n print \"Enter a number: \"\n x = 123\n \n", + "input": [ + "\n", + "\n", + "\n", + "#Variable decleration\n", + "x=0\n", + "\n", + "#Loop till 123 is entered\n", + "while x!=123:\n", + " print \"Enter a number: \"\n", + " x = 123\n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Enter a number: \n" + "text": [ + "Enter a number: \n" + ] } ], "prompt_number": 11 @@ -162,19 +417,48 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.9, Page Number: 68<h3>" + "source": [ + "<h3>Example 4.9, Page Number: 68<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Demonstrate switch usig \"help\" program'''\n\n#Displaying the menu\nprint \"Help on:\"\nprint \"1. for\"\nprint \"2. if\"\nprint \"3. while\"\n\n\nchoice = 2 #Choice of user\n\nif choice==1: #Executing users choice with if-else\n print \"for is c++'s most versatile loop.\"\nelif choice==2:\n print \"if is c++'s conditional branch statement.\"\nelif choice==3:\n print \"switch is C++'s multiway branch statement. \"\nelse:\n print \"You must enter a number between 1 and 3.\"\n \n", + "input": [ + "\n", + "\n", + "#Displaying the menu\n", + "print \"Help on:\"\n", + "print \"1. for\"\n", + "print \"2. if\"\n", + "print \"3. while\"\n", + "\n", + "\n", + "choice = 2 #Choice of user\n", + "\n", + "if choice==1: #Executing users choice with if-else\n", + " print \"for is c++'s most versatile loop.\"\n", + "elif choice==2:\n", + " print \"if is c++'s conditional branch statement.\"\n", + "elif choice==3:\n", + " print \"switch is C++'s multiway branch statement. \"\n", + "else:\n", + " print \"You must enter a number between 1 and 3.\"\n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Help on:\n1. for\n2. if\n3. while\nif is c++'s conditional branch statement.\n" + "text": [ + "Help on:\n", + "1. for\n", + "2. if\n", + "3. while\n", + "if is c++'s conditional branch statement.\n" + ] } ], "prompt_number": 19 @@ -182,19 +466,42 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.10, Page Number: 69<h3>" + "source": [ + "<h3>Example 4.10, Page Number: 69<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementing the switch statement'''\n#If switch without break is used(as in the book), \n#it gives a different output(Fall through).'''\n\nfor i in range(5):\n if i==0:\n print \"less than 1\"\n elif i==1:\n print \"less than 2\"\n elif i==2:\n print \"less than 3\"\n elif i==3:\n print \"less than 4\"\n elif i==4:\n print \"less than 5\"\n\n", + "input": [ + "\n", + "\n", + "for i in range(5):\n", + " if i==0:\n", + " print \"less than 1\"\n", + " elif i==1:\n", + " print \"less than 2\"\n", + " elif i==2:\n", + " print \"less than 3\"\n", + " elif i==3:\n", + " print \"less than 4\"\n", + " elif i==4:\n", + " print \"less than 5\"\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "less than 1\nless than 2\nless than 3\nless than 4\nless than 5\n" + "text": [ + "less than 1\n", + "less than 2\n", + "less than 3\n", + "less than 4\n", + "less than 5\n" + ] } ], "prompt_number": 20 @@ -202,19 +509,167 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.11, Page Number: 71<h3>" + "source": [ + "<h3>Example 4.11, Page Number: 71<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Displays all printable characters.'''\n\n#Variable decleration\nch = 32\n\n#Loop to print the characters\nfor ch in range(128):\n print chr(ch)\n \n", + "input": [ + "\n", + "'''Displays all printable characters.'''\n", + "\n", + "#Variable decleration\n", + "ch = 32\n", + "\n", + "#Loop to print the characters\n", + "for ch in range(128):\n", + " print chr(ch)\n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "\u0000\n\u0001\n\u0002\n\u0003\n\u0004\n\u0005\n\u0006\n\u0007\n\b\n\t\n\n\n\u000b\n\f\n\r\n\u000e\n\u000f\n\u0010\n\u0011\n\u0012\n\u0013\n\u0014\n\u0015\n\u0016\n\u0017\n\u0018\n\u0019\n\u001a\n\u001b\n\u001c\n\u001d\n\u001e\n\u001f\n \n!\n\"\n#\n$\n%\n&\n'\n(\n)\n*\n+\n,\n-\n.\n/\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n:\n;\n<\n=\n>\n?\n@\nA\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ\n[\n\\\n]\n^\n_\n`\na\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n{\n|\n}\n~\n\u007f\n" + "text": [ + "\u0000\n", + "\u0001\n", + "\u0002\n", + "\u0003\n", + "\u0004\n", + "\u0005\n", + "\u0006\n", + "\u0007\n", + "\b\n", + "\t\n", + "\n", + "\n", + "\u000b", + "\n", + "\f", + "\n", + "\r\n", + "\u000e\n", + "\u000f\n", + "\u0010\n", + "\u0011\n", + "\u0012\n", + "\u0013\n", + "\u0014\n", + "\u0015\n", + "\u0016\n", + "\u0017\n", + "\u0018\n", + "\u0019\n", + "\u001a\n", + "\u001b\n", + "\u001c", + "\n", + "\u001d", + "\n", + "\u001e", + "\n", + "\u001f\n", + " \n", + "!\n", + "\"\n", + "#\n", + "$\n", + "%\n", + "&\n", + "'\n", + "(\n", + ")\n", + "*\n", + "+\n", + ",\n", + "-\n", + ".\n", + "/\n", + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + ":\n", + ";\n", + "<\n", + "=\n", + ">\n", + "?\n", + "@\n", + "A\n", + "B\n", + "C\n", + "D\n", + "E\n", + "F\n", + "G\n", + "H\n", + "I\n", + "J\n", + "K\n", + "L\n", + "M\n", + "N\n", + "O\n", + "P\n", + "Q\n", + "R\n", + "S\n", + "T\n", + "U\n", + "V\n", + "W\n", + "X\n", + "Y\n", + "Z\n", + "[\n", + "\\\n", + "]\n", + "^\n", + "_\n", + "`\n", + "a\n", + "b\n", + "c\n", + "d\n", + "e\n", + "f\n", + "g\n", + "h\n", + "i\n", + "j\n", + "k\n", + "l\n", + "m\n", + "n\n", + "o\n", + "p\n", + "q\n", + "r\n", + "s\n", + "t\n", + "u\n", + "v\n", + "w\n", + "x\n", + "y\n", + "z\n", + "{\n", + "|\n", + "}\n", + "~\n", + "\u007f\n" + ] } ], "prompt_number": 21 @@ -222,19 +677,35 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.12, Page Number: 72<h3>" + "source": [ + "<h3>Example 4.12, Page Number: 72<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Example for while loop'''\n\n#Variable decleration\nlen = 5\n\nwhile (len>0 & len<80):\n print \".\"\n len-=1\n", + "input": [ + "\n", + "#Variable decleration\n", + "len = 5\n", + "\n", + "while (len>0 & len<80):\n", + " print \".\"\n", + " len-=1\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": ".\n.\n.\n.\n.\n" + "text": [ + ".\n", + ".\n", + ".\n", + ".\n", + ".\n" + ] } ], "prompt_number": 22 @@ -242,19 +713,38 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.13, Page Number: 73<h3>" + "source": [ + "<h3>Example 4.13, Page Number: 73<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Program to loop till number 100 is entered'''\n\n#Variable Declaration\nnum=95\n\nwhile True:\n print \"Enter a number(100 to stop): \"\n num+=1 #User input, incrementing num till it reaches 100\n if(num==100): #Condition check to stop loop\n break\n", + "input": [ + "\n", + "\n", + "#Variable Declaration\n", + "num=95\n", + "\n", + "while True:\n", + " print \"Enter a number(100 to stop): \"\n", + " num+=1 #User input, incrementing num till it reaches 100\n", + " if(num==100): #Condition check to stop loop\n", + " break\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Enter a number(100 to stop): \nEnter a number(100 to stop): \nEnter a number(100 to stop): \nEnter a number(100 to stop): \nEnter a number(100 to stop): \n" + "text": [ + "Enter a number(100 to stop): \n", + "Enter a number(100 to stop): \n", + "Enter a number(100 to stop): \n", + "Enter a number(100 to stop): \n", + "Enter a number(100 to stop): \n" + ] } ], "prompt_number": 9 @@ -262,19 +752,79 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.14, Page Number: 73<h3>" + "source": [ + "<h3>Example 4.14, Page Number: 73<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "\n'''Magic Number program: 3rd improvement.'''\n\nimport random\n\n#Variable decleration\nmagic = random.randint(0,100) #Number which the user has to guess\nlow=0\nhigh=100\n\n#Play thr magic number game\nwhile True:\n guess = random.randint(low,high) #Number which the user guesses\n if guess==magic: \n print \"**Right**\"\n print magic,\" is the magic number.\"\n break\n else:\n print \"...Sorry, you're wrong.\"\n if(guess>magic):\n print \"Your guess is too high.\"\n high=guess\n else:\n print \"Your guess is too low.\"\n low=guess\n \n \n", + "input": [ + "\n", + "\n", + "import random\n", + "\n", + "#Variable decleration\n", + "magic = random.randint(0,100) #Number which the user has to guess\n", + "low=0\n", + "high=100\n", + "\n", + "#Play thr magic number game\n", + "while True:\n", + " guess = random.randint(low,high) #Number which the user guesses\n", + " if guess==magic: \n", + " print \"**Right**\"\n", + " print magic,\" is the magic number.\"\n", + " break\n", + " else:\n", + " print \"...Sorry, you're wrong.\"\n", + " if(guess>magic):\n", + " print \"Your guess is too high.\"\n", + " high=guess\n", + " else:\n", + " print \"Your guess is too low.\"\n", + " low=guess\n", + " \n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "...Sorry, you're wrong.\nYour guess is too low.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too low.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too low.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too low.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too low.\n...Sorry, you're wrong.\nYour guess is too low.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too high.\n...Sorry, you're wrong.\nYour guess is too low.\n**Right**\n72 is the magic number.\n" + "text": [ + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too high.\n", + "...Sorry, you're wrong.\n", + "Your guess is too low.\n", + "**Right**\n", + "72 is the magic number.\n" + ] } ], "prompt_number": 26 @@ -282,19 +832,32 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.15, Page Number: 74<h3>" + "source": [ + "<h3>Example 4.15, Page Number: 74<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Example for continue statement'''\n\nfor x in range(100+1):\n if x%2: #Condition check to continue the loop\n continue\n print x,\n \n\n", + "input": [ + "\n", + "\n", + "for x in range(100+1):\n", + " if x%2: #Condition check to continue the loop\n", + " continue\n", + " print x,\n", + " \n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100\n" + "text": [ + "0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100\n" + ] } ], "prompt_number": 2 @@ -302,19 +865,31 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.16, Page Number: 75<h3>" + "source": [ + "<h3>Example 4.16, Page Number: 75<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Iterates from 0 to 9, not to 100'''\n\nfor t in range(100):\n if t==10: #Condition check to break out of the loop\n break\n print t,\n \n", + "input": [ + "\n", + "\n", + "for t in range(100):\n", + " if t==10: #Condition check to break out of the loop\n", + " break\n", + " print t,\n", + " \n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "0 1 2 3 4 5 6 7 8 9\n" + "text": [ + "0 1 2 3 4 5 6 7 8 9\n" + ] } ], "prompt_number": 3 @@ -322,19 +897,134 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.17, Page Number: 75<h3>" + "source": [ + "<h3>Example 4.17, Page Number: 75<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''An example for break'''\n\nfor t in range(100):\n count = 1 \n while True:\n print count,\n count+=1\n if count==10: \n break #Breaks from the inner loop\n print\n\n", + "input": [ + "\n", + "\n", + "for t in range(100):\n", + " count = 1 \n", + " while True:\n", + " print count,\n", + " count+=1\n", + " if count==10: \n", + " break #Breaks from the inner loop\n", + " print\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n1 2 3 4 5 6 7 8 9\n" + "text": [ + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n", + "1 2 3 4 5 6 7 8 9\n" + ] } ], "prompt_number": 5 @@ -342,24 +1032,206 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.18, Page Number: 76<h3>" + "source": [ + "<h3>Example 4.18, Page Number: 76<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''This Program finds the prime nos. from 2 to 1000'''\n\nfor i in range(2,1000):\n j=2\n for j in range(2,i/j): #Nested for loop\n if i%j == False: #Check for prime no.\n break\n if j>(i/j):\n print i,\" is prime.\" #Result\n\n", + "input": [ + "\n", + "\n", + "for i in range(2,1000):\n", + " j=2\n", + " for j in range(2,i/j): #Nested for loop\n", + " if i%j == False: #Check for prime no.\n", + " break\n", + " if j>(i/j):\n", + " print i,\" is prime.\" #Result\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "2 is prime.\n3 is prime.\n11 is prime.\n13 is prime.\n17 is prime.\n19 is prime.\n23 is prime.\n29 is prime.\n31 is prime.\n37 is prime.\n41 is prime.\n43 is prime.\n47 is prime.\n53 is prime.\n59 is prime.\n61 is prime.\n67 is prime.\n71 is prime.\n73 is prime.\n79 is prime.\n83 is prime.\n89 is prime.\n97 is prime.\n101 is prime.\n103 is prime.\n107 is prime.\n109 is prime.\n113 is prime.\n127 is prime.\n131 is prime.\n137 is prime.\n139 is prime.\n149 is prime.\n151 is prime.\n157 is prime.\n163 is prime.\n167 is prime.\n173 is prime.\n179 is prime.\n181 is prime.\n191 is prime.\n193 is prime.\n197 is prime.\n199 is prime.\n211 is prime.\n223 is prime.\n227 is prime.\n229 is prime.\n233 is prime.\n239 is prime.\n241 is prime.\n251 is prime.\n257 is prime.\n263 is prime.\n269 is prime.\n271 is prime.\n277 is prime.\n281 is prime.\n283 is prime.\n293 is prime.\n307 is prime.\n311 is prime.\n313 is prime.\n317 is prime.\n331 is prime.\n337 is prime.\n347 is prime.\n349 is prime.\n353 is prime.\n359 is prime.\n367 is prime.\n373 is prime.\n379 is prime.\n383 is prime.\n389 is prime.\n397 is prime.\n401 is prime.\n409 is prime.\n419 is prime.\n421 is prime.\n431 is prime.\n433 is prime.\n439 is prime.\n443 is prime.\n449 is prime.\n457 is prime.\n461 is prime.\n463 is prime.\n467 is prime.\n479 is prime.\n487 is prime.\n491 is prime.\n499 is prime.\n503 is prime.\n509 is prime.\n521 is prime.\n523 is prime.\n541 is prime.\n547 is prime.\n557 is prime.\n563 is prime.\n569 is prime.\n571 is prime.\n577 is prime.\n587 is prime.\n593 is prime.\n599 is prime.\n601 is prime.\n607 is prime.\n613 is prime.\n617 is prime.\n619 is prime.\n631 is prime.\n641 is prime.\n643 is prime.\n647 is prime.\n653 is prime.\n659 is prime.\n661 is prime.\n673 is prime.\n677 is prime.\n683 is prime.\n691 is prime.\n701 is prime.\n709 is prime.\n719 is prime.\n727 is prime.\n733 is prime.\n739 is prime.\n743 is prime.\n751 is prime.\n757 is prime.\n761 is prime.\n769 is prime.\n773 is prime.\n787 is prime.\n797 is prime.\n809 is prime.\n811 is prime.\n821 is prime.\n823 is prime.\n827 is prime.\n829 is prime.\n839 is prime.\n853 is prime.\n857 is prime.\n859 is prime.\n863 is prime.\n877" + "text": [ + "2 is prime.\n", + "3 is prime.\n", + "11 is prime.\n", + "13 is prime.\n", + "17 is prime.\n", + "19 is prime.\n", + "23 is prime.\n", + "29 is prime.\n", + "31 is prime.\n", + "37 is prime.\n", + "41 is prime.\n", + "43 is prime.\n", + "47 is prime.\n", + "53 is prime.\n", + "59 is prime.\n", + "61 is prime.\n", + "67 is prime.\n", + "71 is prime.\n", + "73 is prime.\n", + "79 is prime.\n", + "83 is prime.\n", + "89 is prime.\n", + "97 is prime.\n", + "101 is prime.\n", + "103 is prime.\n", + "107 is prime.\n", + "109 is prime.\n", + "113 is prime.\n", + "127 is prime.\n", + "131 is prime.\n", + "137 is prime.\n", + "139 is prime.\n", + "149 is prime.\n", + "151 is prime.\n", + "157 is prime.\n", + "163 is prime.\n", + "167 is prime.\n", + "173 is prime.\n", + "179 is prime.\n", + "181 is prime.\n", + "191 is prime.\n", + "193 is prime.\n", + "197 is prime.\n", + "199 is prime.\n", + "211 is prime.\n", + "223 is prime.\n", + "227 is prime.\n", + "229 is prime.\n", + "233 is prime.\n", + "239 is prime.\n", + "241 is prime.\n", + "251 is prime.\n", + "257 is prime.\n", + "263 is prime.\n", + "269 is prime.\n", + "271 is prime.\n", + "277 is prime.\n", + "281 is prime.\n", + "283 is prime.\n", + "293 is prime.\n", + "307 is prime.\n", + "311 is prime.\n", + "313 is prime.\n", + "317 is prime.\n", + "331 is prime.\n", + "337 is prime.\n", + "347 is prime.\n", + "349 is prime.\n", + "353 is prime.\n", + "359 is prime.\n", + "367 is prime.\n", + "373 is prime.\n", + "379 is prime.\n", + "383 is prime.\n", + "389 is prime.\n", + "397 is prime.\n", + "401 is prime.\n", + "409 is prime.\n", + "419 is prime.\n", + "421 is prime.\n", + "431 is prime.\n", + "433 is prime.\n", + "439 is prime.\n", + "443 is prime.\n", + "449 is prime.\n", + "457 is prime.\n", + "461 is prime.\n", + "463 is prime.\n", + "467 is prime.\n", + "479 is prime.\n", + "487 is prime.\n", + "491 is prime.\n", + "499 is prime.\n", + "503 is prime.\n", + "509 is prime.\n", + "521 is prime.\n", + "523 is prime.\n", + "541 is prime.\n", + "547 is prime.\n", + "557 is prime.\n", + "563 is prime.\n", + "569 is prime.\n", + "571 is prime.\n", + "577 is prime.\n", + "587 is prime.\n", + "593 is prime.\n", + "599 is prime.\n", + "601 is prime.\n", + "607 is prime.\n", + "613 is prime.\n", + "617 is prime.\n", + "619 is prime.\n", + "631 is prime.\n", + "641 is prime.\n", + "643 is prime.\n", + "647 is prime.\n", + "653 is prime.\n", + "659 is prime.\n", + "661 is prime.\n", + "673 is prime.\n", + "677 is prime.\n", + "683 is prime.\n", + "691 is prime.\n", + "701 is prime.\n", + "709 is prime.\n", + "719 is prime.\n", + "727 is prime.\n", + "733 is prime.\n", + "739 is prime.\n", + "743 is prime.\n", + "751 is prime.\n", + "757 is prime.\n", + "761 is prime.\n", + "769 is prime.\n", + "773 is prime.\n", + "787 is prime.\n", + "797 is prime.\n", + "809 is prime.\n", + "811 is prime.\n", + "821 is prime.\n", + "823 is prime.\n", + "827 is prime.\n", + "829 is prime.\n", + "839 is prime.\n", + "853 is prime.\n", + "857 is prime.\n", + "859 is prime.\n", + "863 is prime.\n", + "877" + ] }, { "output_type": "stream", "stream": "stdout", - "text": " is prime.\n881 is prime.\n883 is prime.\n887 is prime.\n907 is prime.\n911 is prime.\n919 is prime.\n929 is prime.\n937 is prime.\n941 is prime.\n947 is prime.\n953 is prime.\n967 is prime.\n971 is prime.\n977 is prime.\n983 is prime.\n991 is prime.\n997 is prime.\n" + "text": [ + " is prime.\n", + "881 is prime.\n", + "883 is prime.\n", + "887 is prime.\n", + "907 is prime.\n", + "911 is prime.\n", + "919 is prime.\n", + "929 is prime.\n", + "937 is prime.\n", + "941 is prime.\n", + "947 is prime.\n", + "953 is prime.\n", + "967 is prime.\n", + "971 is prime.\n", + "977 is prime.\n", + "983 is prime.\n", + "991 is prime.\n", + "997 is prime.\n" + ] } ], "prompt_number": 30 @@ -367,19 +1239,92 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 4.19, Page Number: 78<h3>" + "source": [ + "<h3>Example 4.19, Page Number: 78<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Magic Number program: Final improvement'''\n\nimport random\n\n#Variable declaration\nmagic = random.randint(0,100) #Number to be guessed\ni=1\nhigh=100\nlow=0\n\n#Function to play the magic number game\ndef play(m):\n low=0\n high=100\n for t in range(100):\n x = random.randint(low,high) #Number guessed by the user\n if x==m:\n print \"***Right***\"\n return\n else:\n if(x<m):\n print \"Too low.\"\n low=x\n else:\n print \"Too high.\"\n high=x\n print \"You've used up all your guesses\" \n\n#Menu\nwhile True:\n print \"1.Get a new magic number.\" \n print \"2.Play\"\n print \"3.Quit\"\n while True:\n option = i \n if option>=1 and option<=3:\n break\n if option==1:\n magic=random.randint(0,100) #Number to be guessed\n elif option==2:\n play(magic) #Calls the function play\n elif option==3:\n print \"Goodbye\" \n break\n i+=1 #increments i such that the 3 options get selected sequentially\n", + "input": [ + "\n", + "\n", + "import random\n", + "\n", + "#Variable declaration\n", + "magic = random.randint(0,100) #Number to be guessed\n", + "i=1\n", + "high=100\n", + "low=0\n", + "\n", + "#Function to play the magic number game\n", + "def play(m):\n", + " low=0\n", + " high=100\n", + " for t in range(100):\n", + " x = random.randint(low,high) #Number guessed by the user\n", + " if x==m:\n", + " print \"***Right***\"\n", + " return\n", + " else:\n", + " if(x<m):\n", + " print \"Too low.\"\n", + " low=x\n", + " else:\n", + " print \"Too high.\"\n", + " high=x\n", + " print \"You've used up all your guesses\" \n", + "\n", + "#Menu\n", + "while True:\n", + " print \"1.Get a new magic number.\" \n", + " print \"2.Play\"\n", + " print \"3.Quit\"\n", + " while True:\n", + " option = i \n", + " if option>=1 and option<=3:\n", + " break\n", + " if option==1:\n", + " magic=random.randint(0,100) #Number to be guessed\n", + " elif option==2:\n", + " play(magic) #Calls the function play\n", + " elif option==3:\n", + " print \"Goodbye\" \n", + " break\n", + " i+=1 #increments i such that the 3 options get selected sequentially\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1.Get a new magic number.\n2.Play\n3.Quit\n1.Get a new magic number.\n2.Play\n3.Quit\nToo low.\nToo low.\nToo low.\nToo low.\nToo high.\nToo low.\nToo high.\nToo low.\nToo high.\nToo high.\nToo high.\nToo high.\nToo low.\n***Right***\n1.Get a new magic number.\n2.Play\n3.Quit\nGoodbye\n" + "text": [ + "1.Get a new magic number.\n", + "2.Play\n", + "3.Quit\n", + "1.Get a new magic number.\n", + "2.Play\n", + "3.Quit\n", + "Too low.\n", + "Too low.\n", + "Too low.\n", + "Too low.\n", + "Too high.\n", + "Too low.\n", + "Too high.\n", + "Too low.\n", + "Too high.\n", + "Too high.\n", + "Too high.\n", + "Too high.\n", + "Too low.\n", + "***Right***\n", + "1.Get a new magic number.\n", + "2.Play\n", + "3.Quit\n", + "Goodbye\n" + ] } ], "prompt_number": 6 @@ -387,7 +1332,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |