{ "metadata": { "name": "", "signature": "sha256:c565a0de8f316cbc19d18a7bdba735b6241f6d0c0c10c2d49245aa88403df9e7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6, The While and do...While Loops" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-1 , Page number: 86" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A first look at the while loop\n", "\n", "i = 1\n", "\n", "while i < 11:\n", "\tprint \"This is line number %d\"%i\n", "\ti += 1 \t\t\t# Python doesn't support \"++\" or \"--\" shorthand expressions" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is line number 1\n", "This is line number 2\n", "This is line number 3\n", "This is line number 4\n", "This is line number 5\n", "This is line number 6\n", "This is line number 7\n", "This is line number 8\n", "This is line number 9\n", "This is line number 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-2 , Page number: 89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Countdown to blastoff\n", "\n", "i = 10\n", "\n", "print \"Countdown\"\n", "\n", "while i >= 0 :\n", "\tprint \"%d\"%i\n", "\ti -= 1\n", "\n", "print \"\\n\\nBlastoff!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Countdown\n", "10\n", "9\n", "8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "2\n", "1\n", "0\n", "\n", "\n", "Blastoff!\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-3 , Page number: 90" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Hailstones\n", "\n", "counter = 0;\n", "\n", "print \"Enter a positive integer:\",\n", "n = 51\n", "print n\n", "\n", "nsave = n\n", "\n", "\n", "while n > 1:\n", "\tif n % 2 :\t\t\t# if (n % 2) is non-zero, n is not divisible by 2 and is therefore odd\n", "\t\tn = n * 3 + 1\n", "\telse :\n", "\t\tn /= 2\n", "\tcounter += 1\n", "\tprint \"Step %4d: n = %4d\"%(counter,n)\n", "\n", "print \"\\n\\n%d went to 1 in %d steps.\"%(nsave,counter)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: 51\n", "Step 1: n = 154\n", "Step 2: n = 77\n", "Step 3: n = 232\n", "Step 4: n = 116\n", "Step 5: n = 58\n", "Step 6: n = 29\n", "Step 7: n = 88\n", "Step 8: n = 44\n", "Step 9: n = 22\n", "Step 10: n = 11\n", "Step 11: n = 34\n", "Step 12: n = 17\n", "Step 13: n = 52\n", "Step 14: n = 26\n", "Step 15: n = 13\n", "Step 16: n = 40\n", "Step 17: n = 20\n", "Step 18: n = 10\n", "Step 19: n = 5\n", "Step 20: n = 16\n", "Step 21: n = 8\n", "Step 22: n = 4\n", "Step 23: n = 2\n", "Step 24: n = 1\n", "\n", "\n", "51 went to 1 in 24 steps.\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-4 , Page number: 92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Computing the factorial\n", "\n", "print \"Type in a positive integer:\",\n", "n = 6\n", "print n\n", "\n", "nsave = n\n", "factorial = 1\n", "\n", "while n > 1 :\t\n", "\tfactorial *= n\n", "\tn -= 1\n", "\n", "print \"Factorial of %d = %d\"%(nsave,factorial)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in a positive integer: 6\n", "Factorial of 6 = 720\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-5 , Page number: 93" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Computing the factorial (shorter version)\n", "\n", "print \"Type in a positive integer: \",\n", "n = 6\n", "print n\n", "\n", "nsave = n\n", "factorial = 1\n", "\n", "while n > 1 :\n", "\tfactorial *= n # Since python doesn't support '--' expression, the given program cannot be shortened\n", "\tn -= 1\n", "\n", "print \"Factorial of %d = %d\"%(nsave,factorial)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in a positive integer: 6\n", "Factorial of 6 = 720\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-6 , Page number: 95" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Illustration of do..while loops\n", "\n", "# This program reads in an integer, adds up the values of each of its individual digits, and prints out the sum.\n", "\n", "sum = 0\n", "\n", "print \"Type in an integer:\",\n", "n = 1776\n", "print n\n", "\n", "# There is no do...while loop in python\n", "# An equivalent while loop can work\n", "\n", "while n > 0 :\n", "\trightmost_digit = n % 10\t# Extract rightmost digit\n", "\tsum += rightmost_digit\n", "\tn /= 10\t\t\t\t\t\t# Move next digit into rightmost position\n", "\n", "print \"The sum of the digits is %d\"%sum\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in an integer: 1776\n", "The sum of the digits is 21\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-7 , Page number: 97" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "# This program reads in an integer and prints it out backwards\n", "\n", "print \"Type in an integer: \",\n", "n = 5746\n", "print n\n", "\n", "print \"%d backwards is \"%n,\n", "\n", "while n > 0 :\n", "\trightmost_digit = n % 10\n", "\tsys.stdout.write(\"%d\"%rightmost_digit) # For printing in the same line without implicit whitespaces\n", "\tn /= 10\n", "print \t\t\t# For the new line\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in an integer: 5746\n", "5746 backwards is 6475\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-8 , Page number: 99" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A program to read in numbers and print out their squares\n", "\n", "print \"Enter an integer (negative to quit):\",\n", "n = 5\n", "print n\n", "\n", "while n > 0 :\n", "\tprint \"%d squared is %d\"%(n,n*n)\n", "\tprint \"\\nEnter an integer (negative to quit):\",\n", "\tn = -1\n", "\tprint n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter an integer (negative to quit): 5\n", "5 squared is 25\n", "\n", "Enter an integer (negative to quit): -1\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-9 , Page number: 100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A program to read in radius values and print out the circumference and area\n", "\n", "pi = 3.1416\n", "\n", "print \"Enter radius (negative to quit):\",\n", "radius = 4.5\n", "print radius\n", "\n", "while radius >= 0.0 :\n", "\tprint \"The circumference is %f\"%(2.0 * pi * radius)\n", "\tprint \"The area is %f\"%(pi * radius * radius)\n", "\tprint \"\\nEnter radius (negative to quit):\",\n", "\tradius = -1.0\n", "\tprint radius\n", "\t\n", "\t" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter radius (negative to quit): 4.5\n", "The circumference is 28.274400\n", "The area is 63.617400\n", "\n", "Enter radius (negative to quit): -1.0\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6-10 , Page number: 101" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# A program to read in length and width and print out the perimeter and area\n", "\n", "print \"Enter length and width\"\n", "print \" (one or both negative to quit):\",\n", "length = 4\n", "width = 5\n", "print length,width\n", "\n", "while length > 0 and width > 0 : \n", "\tprint \"The perimeter is %d\"%(2*(length+width))\n", "\tprint \"The area is %d\"%(length*width)\n", "\tprint \"\\nEnter length and width\"\n", "\tprint \" (one or both negative to quit):\",\n", "\tlength = -1\n", "\twidth = 0\n", "\tprint length,width\t" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter length and width\n", " (one or both negative to quit): 4 5\n", "The perimeter is 18\n", "The area is 20\n", "\n", "Enter length and width\n", " (one or both negative to quit): -1 0\n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }