{ "metadata": { "name": "", "signature": "sha256:527aef20f7b40df168e6ff4a5ccb96d249aaf7c6743cc854ce1a221227ecfb75" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5 - Making Decisions: if and switch Statements" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.1, page no. 100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "a = 4\n", "b = 5\n", "print a, \" > \", b, \" is \", (a > b)\n", "print a, \" >= \", b, \" is \", (a >= b)\n", "print a, \" == \", b, \" is \", (a == b)\n", "print a, \" <= \", b, \" is \", (a <= b)\n", "print a, \" < \", b, \" is \", (a < b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 > 5 is False\n", "4 >= 5 is False\n", "4 == 5 is False\n", "4 <= 5 is True\n", "4 < 5 is True\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.2, page no. 102" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter number of preregistered students: \",\n", "total = int(raw_input())\n", "print \"Enter number of students adding the course: \",\n", "added = int(raw_input())\n", "total = total + added\n", "print \"Total number of students: \", total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter number of preregistered students: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "30\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter number of students adding the course: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total number of students: 33\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.3, page no. 104" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a whole number: \",\n", "num = int(raw_input())\n", "if (num % 2 == 0):\n", " print \"The number is even\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a whole number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number is even\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.4, page no. 108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a whole number: \",\n", "num = int(raw_input())\n", "if (num % 2 == 0):\n", " print \"The number is even\"\n", "else:\n", " print \"The number is odd\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a whole number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number is odd\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.4, page no. 109" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a whole number: \",\n", "num = int(raw_input())\n", "print \"The number is\", (\"even\" if num %2 == 0 else \"odd\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a whole number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number is even\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.6, page no. 112" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your test score: \",\n", "testScore = int(raw_input())\n", "if (testScore >= 90 ):\n", " print \"Your grade is an A\"\n", "elif (testScore >= 80 ):\n", " print \"Your grade is a B\"\n", "elif (testScore >= 70 ):\n", " print \"Your grade is a C\"\n", "elif (testScore >= 60 ):\n", " print \"Your grade is a D\"\n", "else:\n", " print \"Your grade is an F\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your test score: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "70\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your grade is a C\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.7, page no. 115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your grade: \",\n", "grade = raw_input()\n", "\n", "if grade == 'A':\n", " print \"Your average must be between 90 - 100\"\n", "elif grade == 'B':\n", " print \"Your average must be between 80 - 89\"\n", "elif grade == 'C':\n", " print \"Your average must be between 70 - 79\"\n", "elif grade == 'D':\n", " print \"Your average must be between 60 - 69\"\n", "else:\n", " print \"Your average must be below 60\" " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "D\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your average must be between 60 - 69\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.8, page no. 118" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "print \"Enter your grade: \",\n", "grade = raw_input()\n", "\n", "if grade == 'A' or grade == 'a':\n", " print \"Your average must be between 90 - 100\"\n", "elif grade == 'B' or grade == 'b':\n", " print \"Your average must be between 80 - 89\"\n", "elif grade == 'C' or grade == 'c':\n", " print \"Your average must be between 70 - 79\"\n", "elif grade == 'D' or grade == 'd':\n", " print \"Your average must be between 60 - 69\"\n", "else:\n", " print \"Your average must be below 60\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "d\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your average must be between 60 - 69\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.9, page no. 119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "print \"Choose your car\"\n", "print \"S for Standard\"\n", "print \"L for Leather Seats\"\n", "print \"D for Leather Seats + Chrome Wheels\"\n", "choice = raw_input()\n", "print \"Extra features purchased\"\n", "if choice == 'D':\n", " print \"Chrome wheels\\n\"\n", "elif choice == 'L':\n", " print \"Leather seats\"\n", "else:\n", " print \"None selected\"\n" ], "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/home/jovina/TBC/hardik/c++-demystified/\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"L for Leather Seats\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"D for Leather Seats + Chrome Wheels\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mchoice\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 10\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Extra features purchased\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mchoice\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'D'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/home/jovina/epd/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 251\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 252\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 253\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 254\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 255\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/jovina/epd/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 443\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 444\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 445\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 446\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 447\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": [ "Choose your car\n", "S for Standard\n", "L for Leather Seats\n", "D for Leather Seats + Chrome Wheels\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }