{ "metadata": { "name": "ch3" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3: Selection" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.1, page no. 36" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two positive integers: \";\n", "n = int(raw_input())\n", "d = int(raw_input())\n", "if (n%d):\n", " print \"%d is not divisible by %d\" %(n,d)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two positive integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "66\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "66 is not divisible by 7\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.2, page no. 37" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two positive integers: \";\n", "n = int(raw_input())\n", "d = int(raw_input())\n", "if (n%d):\n", " print \"%d is not divisible by %d\" %(n,d)\n", "else:\n", " print \"%d is divisible by %d\" %(n,d)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two positive integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "56\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "56 is divisible by 7\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.3, page no. 38" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two integers: \"\n", "m = int(raw_input())\n", "n = int(raw_input())\n", "\n", "if (m < n):\n", " print \"%d is the minimum.\" %m\n", "else:\n", " print \"%d is the minimum.\" %n\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "55 is the minimum.\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.4, page no. 38" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter an integer: \"\n", "n = int(raw_input())\n", "if (n = 22):\n", " print \"%d = 22\" %n\n", "else: \n", " print \"%d != 22\" %n" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 3)", "output_type": "pyerr", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m if (n = 22):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.5, page no. 39" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter three integers: \"\n", "n1 = int(raw_input())\n", "n2 = int(raw_input())\n", "n3 = int(raw_input())\n", "\n", "m=n1\n", "# now min <= n1\n", "if (n2 < m):\n", " m = n2 # now min <= n1 and min <= n2\n", "if (n3 < m):\n", " m = n3 # now min <= n1, min <= n2, and min <= n3\n", "print \"Their minimum is %d\" % m" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "33\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Their minimum is 33\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.6, page no. 39" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two integers: \"\n", "x = int(raw_input())\n", "y = int(raw_input())\n", "\n", "if (x > y):\n", " temp=x\n", " x = y\n", " y = temp\n", " \n", "\n", "print \"%d <= %d\" %(x,y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "66\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "44\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "44 <= 66\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.7, page no. 40" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n=44\n", "print \"n = %d\" % n \n", "if True:\n", " # scope extends over 4 lines\n", " print \"Enter an integer: \"\n", " n = int(raw_input())\n", " print \"n = %d\" % n \n", "\n", "if True:\n", " print \"n = %d\" % n \n", " # the n that was declared first\n", "if True:\n", " print \"n = %d\" % n \n", "\n", "print \"n = %d\" % n " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "n = 44\n", "Enter an integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "n = 77\n", "n = 77\n", "n = 77\n", "n = 77\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.8, page no. 41" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter three integers: \"\n", "n1 = int(raw_input())\n", "n2 = int(raw_input())\n", "n3 = int(raw_input())\n", "if (n1 <= n2 and n1 <= n3):\n", " print \"Their minimum is %d\" % n1\n", " \n", "if (n2 <= n1 and n2 <= n3):\n", " print \"Their minimum is %d \" % n2 \n", "if (n3 <= n1 and n3 <= n2):\n", " print \"Their minimum is %d\" % n3 \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "33\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Their minimum is 33 \n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.9, page no. 41" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Are you enrolled (y/n): \"\n", "ans = raw_input()\n", "if (ans == 'Y' or ans == 'y'):\n", " print \"You are enrolled.\\n\"\n", "else: \n", " print \"You are not enrolled.\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Are you enrolled (y/n): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You are enrolled.\n", "\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.10, page no. 42" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter two positive integers: \";\n", "n = int(raw_input())\n", "d = int(raw_input())\n", "\n", "if (d != 0 and n%d == 0): \n", " print \"%d divides %d\" %(d,n)\n", "else:\n", " print \"%d does not divide %d\"% (d,n)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two positive integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "33\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "6 does not divide 33\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.11, page no. 43" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter three integers: \"\n", "n1 = int(raw_input())\n", "n2 = int(raw_input())\n", "n3 = int(raw_input())\n", "\n", "if (n1 >= n2 >= n3):\n", " print \"max = x\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.12, page no. 43" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two positive integers: \"\n", "n = int(raw_input())\n", "d = int(raw_input())\n", "\n", "if (d != 0):\n", " if (n%d == 0):\n", " print d,\n", " print \" divides %d\" % n \n", " else:\n", " print \"%d does not divide %d\" %(d,n)\n", "else:\n", " print '%d does not divide %d '%(d,n)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two positive integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "44\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "44 does not divide 55\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.13, page no. 44" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter three integers: \"\n", "n1 = int(raw_input())\n", "n2 = int(raw_input())\n", "n3 = int(raw_input())\n", "if (n1 < n2):\n", " if (n1 < n3):\n", " print \"Their minimum is : %d\" % n1\n", " else:\n", " print \"Their minimum is : %d\" % n3\n", "else: # n1 >= n2\n", " if (n2 < n3):\n", " print \"Their minimum is : %d\" % n2\n", " else:\n", " print \"Their minimum is %d\" % n3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "33\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Their minimum is : 33\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.14, page no. 44" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Pick a number from 1 to 8.\" \n", "answer = int(raw_input())\n", "print \"Is it less than 5? (y|n): \"\n", "answer = raw_input()\n", "if (answer == 'y'): # 1 <= n <= 4\n", " print \"Is it less than 3? (y|n): \"\n", " answer = raw_input() \n", " if (answer == 'y'): # 1 <= n <= 2\n", " print \"Is it less than 2? (y|n): \"\n", " answer = raw_input()\n", " if (answer == 'y'):\n", " print \"Your number is 1.\"\n", " else:\n", " print \"Your number is 2.\"\n", " else: # 3 <= n <= 4\n", " print \"Is it less than 4? (y|n): \"\n", " answer = raw_input()\n", " if (answer == 'y'):\n", " print \"Your number is 3.\"\n", " else:\n", " print \"Your number is 4.\"\n", "else: # 5 <= n <= 8\n", " print \"Is it less than 7? (y|n): \"\n", " answer = raw_input()\n", " if (answer == 'y'): # 5 <= n <= 6\n", " print \"Is it less than 6? (y|n): \"\n", " answer = raw_input()\n", " if (answer == 'y'):\n", " print \"Your number is 5.\"\n", " else:\n", " print \"Your number is 6.\" \n", " else: # 7 <= n <= 8\n", " print \"Is it less than 8? (y|n): \"\n", " answer = raw_input()\n", " if (answer == 'y'):\n", " print \"Your number is 7.\" \n", " else:\n", " print \"Your number is 8.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Pick a number from 1 to 8.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Is it less than 5? (y|n): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "n\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Is it less than 7? (y|n): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Is it less than 6? (y|n): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "n\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your number is 6.\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.15, page no. 46" ] }, { "cell_type": "code", "collapsed": false, "input": [ "language = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n", "\n", "if (language == 'e'): \n", " print \"Welcome to ProjectEuclid.\"\n", "elif (language == 'f'):\n", " print \"Bon jour, ProjectEuclid.\"\n", "elif (language == 'g'):\n", " print \"Guten tag, ProjectEuclid.\"\n", "elif (language == 'i'):\n", " print \"Bon giorno, ProjectEuclid.\"\n", "elif (language == 'r'):\n", " print \"Dobre utre, ProjectEuclid.\"\n", "else:\n", " print \"Sorry; we don't speak your language.\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Bon giorno, ProjectEuclid.\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.16, page no. 47" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "score = int(raw_input(\"Enter your test score: \"))\n", "a = int(score/10)\n", "if a == 10 or a == 9:\n", " print \"Your grade is an A.\"\n", "elif a == 8:\n", " print \"Your grade is a B.\" \n", "elif a == 7:\n", " print \"Your grade is a C.\" \n", "elif a == 6:\n", " print \"Your grade is a D.\"\n", "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n", " print \"Your grade is an F.\" \n", "else:\n", " print \"Error: score is out of range.\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your test score: 83\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your grade is a B.\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.17, page no. 47" ] }, { "cell_type": "code", "collapsed": false, "input": [ "score = int(raw_input(\"Enter your test score: \"))\n", "a = int(score/10)\n", "if a == 10 or a == 9:\n", " print \"Your grade is an A.\"\n", "elif a == 8:\n", " print \"Your grade is a B.\" \n", "elif a == 7:\n", " print \"Your grade is a C.\" \n", "elif a == 6:\n", " print \"Your grade is a D.\"\n", "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n", " print \"Your grade is an F.\" \n", "else:\n", " print \"Error: score is out of range.\\n\"\n", "\n", "print \"Goodbye.\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your test score: 83\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your grade is a B.\n", "Goodbye.\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.18, page no. 48" ] }, { "cell_type": "code", "collapsed": false, "input": [ "score = int(raw_input(\"Enter your test score: \"))\n", "a = int(score/10)\n", "if a == 10 or a == 9:\n", " print \"Your grade is an A.\"\n", "elif a == 8:\n", " print \"Your grade is a B.\" \n", "elif a == 7:\n", " print \"Your grade is a C.\" \n", "elif a == 6:\n", " print \"Your grade is a D.\"\n", "elif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n", " print \"Your grade is an F.\" \n", "else:\n", " print \"Error: score is out of range.\\n\"\n", "\n", "print \"Goodbye.\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your test score: 83\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your grade is a B.\n", "Goodbye.\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.19, page no. 49" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two integers: \"\n", "m = int(raw_input())\n", "n = int(raw_input())\n", "print min(m,n),\n", "print 'is the minimum'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "33\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "33 is the minimum\n" ] } ], "prompt_number": 19 } ], "metadata": {} } ] }