{ "metadata": { "name": "Chapter VI" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Making decisions" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 6.1, Page number: 66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "number=12 #number=int(raw_input(\"Type in your number: \"))\n", "\n", "try:\n", " if( number < 0 ):\n", " number=-number #change sign,if number is negative\n", "except: \n", " print \"not a number\" #Invalid input/value error\n", " \n", "print (\"The absolute Value is {0}\".format(number))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The absolute Value is 12\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 6.2, Page number: 67" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "gradeTotal=0\n", "failureCount=0\n", "i=0\n", "\n", "numberOfGrades=5 #numberOfGrades=int(raw_input(\"How many grades will you be entering? \"))\n", "grade=[72,83,91,89,95]\n", "\n", "while(i='a' and ch <='z') or (ch>='A' and ch<='Z')):\n", " print(\"It's an alphabetic character.\")\n", "elif(ch>='0'and ch<='9'):\n", " print(\"It's a digit.\")\n", "else:\n", " print(\"It's a special character\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "It's an alphabetic character.\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 6.8, Page number: 80" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "try:\n", " print(\"Type in your expression(with spaces inbetween): \")\n", " value1, operator, value2=\"5 + 2\".split()\n", " #value1, operator, value2=raw_input().split()\n", "\n", "except:\n", " print(\"err.. follow the syntax \") \n", " print(\"with spaces inbetween\\n\")\n", "\n", "value1,value2=[float(value1),float(value2)]\n", "\n", "if(operator=='+'):\n", " print(\"answer: {0:.2f}\".format(value1+value2))\n", "elif(operator=='-'):\n", " print(\"answer: {0:.2f}\".format(value1-value2))\n", "elif(operator=='*'):\n", " print(\"answer: {0:.2f}\".format(value1*value2))\n", "else:\n", " print(\"answer: {0:.2f}\".format(value1/value2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in your expression(with spaces inbetween): \n", "answer: 7.00\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 6.9, Page number: 85" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "try:\n", " print(\"Type in your expression(with spaces inbetween): \")\n", " value1, operator, value2=\"3 * 5\".split()\n", " #value1, operator, value2=raw_input().split()\n", " \n", " \n", "except:\n", " print(\"err.. follow the syntax \") \n", " print(\"with spaces inbetween\\n\")\n", "\n", "value1,value2=[float(value1), float(value2)]\n", "\n", "if(operator=='+'):\n", " print(\"Answer= {0:.2f}\".format(value1+value2))\n", "elif(operator=='-'):\n", " print(\"Answer= {0:.2f}\".format(value1-value2))\n", "elif(operator=='*'):\n", " print(\"Answer= {0:.2f}\".format(value1*value2))\n", "elif(operator=='/'):\n", " if(value2==0):\n", " print(\"Whoops! divide by 0 issue\")\n", " else:\n", " print(\"Answer= {0:.2f}\".format(value1/value2))\n", "else:\n", " print(\"err.. Invalid operator\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Type in your expression(with spaces inbetween): \n", "Answer= 15.00\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 6.10, Page number: 87" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "p=2\n", "\n", "while(p<=50): #Outer loop\n", " isPrime=1 #Variable declaration\n", " d=2 \n", " while(d