{ "metadata": { "name": "", "signature": "sha256:0900a5bcaf7054f0b4351a7d97782dfa44a0ae756b508bf87060112c13f5f981" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 8 - While and Do While Loops" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.1, page no. 161" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for num in range(1, 11):\n", " print num," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5 6 7 8 9 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.2, page no. 161" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 1\n", "while num <=10:\n", " print num,\n", " num += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5 6 7 8 9 10\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.3, page no. 162" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 0\n", "while (num <= 10):\n", " print num,\n", " num += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.4, page no. 163" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a positive number: \",\n", "num = int(raw_input())\n", "while (num <= 0):\n", " print \"Number must be positive; please retry: \",\n", " num = int(raw_input())\n", "print \"The number you entered is \", num, \" \"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number you entered is 4 \n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.5, page no. 164" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a positive number: \",\n", "num = int(raw_input())\n", "while (num <= 0):\n", " print \"Number must be positive; try again (Y/N): \",\n", " choice = raw_input()\n", " if (choice == 'Y'):\n", " print \"Enter number: \",\n", " num = int(raw_input())\n", " else:\n", " break\n", "print \"The number you entered is \", num, \" \"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Number must be positive; try again (Y/N): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number you entered is 7 \n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.6, page no. 165" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "quit = False\n", "print \"Enter a positive number: \",\n", "num = int(raw_input())\n", "while (num <= 0 and quit == False):\n", " print \"Number must be positive; try again (Y/N): \",\n", " choice = raw_input()\n", " if (choice is 'Y'):\n", " print \"Enter number: \",\n", " num = int(raw_input())\n", " else:\n", " quit = True\n", "if (quit == False):\n", " print \"The number you entered is \", num, \" \"\n", "else:\n", " print \"You did not enter a positive number\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number you entered is 3 \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.7, page no. 168" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "quit = False\n", "while(True):\n", " print \"Enter a positive number: \",\n", " num = int(raw_input())\n", " if (num > 0):\n", " break\n", " else:\n", " print \"Number must be positive; try again (Y/N): \",\n", " choice = raw_input()\n", " if (choice != 'Y'):\n", " quit = True\n", " break\n", "if (quit == False):\n", " print \"The number you entered is \", num, \" \"\n", "else:\n", " print \"You did not enter a positive number\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Number must be positive; try again (Y/N): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You did not enter a positive number\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.8, page no. 169" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "counter = 0\n", "total = 0\n", "print \"How many items do you want to buy: \",\n", "num = int(raw_input())\n", "while(counter < num):\n", " counter += 1\n", " if (counter % 13 == 0):\n", " continue\n", " total += 3\n", "print \"Total for \", num, \" items is $\", total" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.9, page no. 169" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "counter = 0\n", "total = 0\n", "print \"How many items do you want to buy: \",\n", "num = int(raw_input())\n", "keepgoing = True\n", "while(counter < num):\n", " counter += 1\n", " if (not(counter % 13 == 0)):\n", " total += 3;\n", "print \"Total for \", num, \" items is $\", total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many items do you want to buy: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total for 4 items is $ 12\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.10, page no. 170" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "x = 0\n", "while(x < 5):\n", " x += 1\n", " y = 0\n", " while(y < 5):\n", " y += 1\n", " print \"X\",\n", " print '\\n'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "X X X X X \n", "\n", "X X X X X \n", "\n", "X X X X X \n", "\n", "X X X X X \n", "\n", "X X X X X \n", "\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 8.11, page no. 171" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "quit = False\n", "print \"Enter a positive number: \",\n", "num = int(raw_input())\n", "while (num <= 0 and quit == False):\n", " print \"Number must be positive; try again (Y/N): \",\n", " choice = raw_input()\n", " if (choice is 'Y'):\n", " print \"Enter number: \",\n", " num = int(raw_input())\n", " else:\n", " quit = True\n", "if (quit == False):\n", " print \"The number you entered is \", num, \" \"\n", "else:\n", " print \"You did not enter a positive number\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number you entered is 3 \n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }