{ "metadata": { "name": "", "signature": "sha256:970f28e2206ea4472dd82a3808592db202377fcd9a01fc69b666e61a221db5b1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10, Arrays" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-1, Page Number : 227" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Introduction to arrays\n", "\n", "array = []\t\t# defining array which is rather called list in python\n", "\n", "print \"You are to enter 5 integers.\\n\"\n", "\n", "# Input the 5 integers into array \"array\"\n", "\n", "for i in range(5) : # equivalent to for i in range(0,5,1)\n", "\tprint \"Enter integer #%d: \"%(i+1),\n", "\tval = int(raw_input())\n", "\tarray.insert(i,val)\t\t\t\t# insert function is used to add elements to list : \n", "\t\t\t\t\t\t\t\t\t# insert(i,val) where i is the index and val is the value to be inserted\n", "\n", "print \"\\nThank you.\\n\"\n", "\n", "# Print out the integers stored in array \"array\"\n", "j = 1\t# used as a counter\n", "for i in array:\t\t\t\t# shorthand way of scanning through the list; using \"in\"\n", "\tprint \"Integer #%d = %d.\"%(j,i)\n", "\tj += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You are to enter 5 integers.\n", "\n", "Enter integer #1:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter integer #2:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter integer #3:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter integer #4:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "63\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter integer #5:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", "Thank you.\n", "\n", "Integer #1 = 3.\n", "Integer #2 = 12.\n", "Integer #3 = 8.\n", "Integer #4 = 63.\n", "Integer #5 = 0.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-2, Page Number : 228" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Initializing a global array\n", "\n", "array = [ 4, 6, 5, 7, 2 ]\n", "\n", "def main() :\t\t\t# just a user-defined function ; not equivalent to the built-in main() function in C\n", "\t\"\"\" prints the integers stored in array \"array\" \"\"\"\n", "\tj = 1\t\t# counter \n", "\tfor i in array :\n", "\t\tprint \"Integer #%d = %d.\"%(j,i)\n", "\t\tj += 1\n", "\n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integer #1 = 4.\n", "Integer #2 = 6.\n", "Integer #3 = 5.\n", "Integer #4 = 7.\n", "Integer #5 = 2.\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-3, Page Number: 230" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# The number of array elements actually used may vary\n", "\n", "# Read in the size of the array\n", "print \"How many integers will you enter?\",\n", "array_size = 6\n", "print array_size\n", "\n", "# Read in the integers\n", "print \"\\nPlease enter %d integers:\"%array_size\n", "array = [ 12, 3, -5, 21, 0, 899 ]\t# Lists dynamically allocate memory \n", "for i in range(array_size) :\n", "\tprint array[i],\n", "# Print out the integers\n", "\n", "print(\"\\n\")\n", "for i in range(array_size) :\n", "\tprint \"Integer #%d = %d.\"%(i+1,array[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many integers will you enter? 6\n", "\n", "Please enter 6 integers:\n", "12 3 -5 21 0 899 \n", "\n", "Integer #1 = 12.\n", "Integer #2 = 3.\n", "Integer #3 = -5.\n", "Integer #4 = 21.\n", "Integer #5 = 0.\n", "Integer #6 = 899.\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-4, Page Number: 231" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# The trailer method\n", "\n", "# Macro definition\n", "MAX_ARRAY_SIZE = 20\n", "TRAILER = -9999\n", "\n", "# Read in the integers\n", "\n", "print \"Please enter up to %d integers, using %d as a trailer:\"%(MAX_ARRAY_SIZE,TRAILER)\n", "array = [ 3, 75, -34, 6, -1, 2, 41, 0, 90, -9999]\n", "for array_size in range(MAX_ARRAY_SIZE) :\n", "\tprint array[array_size],\n", "\tif array[array_size] == -9999 :\n", "\t\tbreak\t\n", "\t\n", "# Print out the integers\n", "print \"\\n\\nYou have entered %d integers\"%array_size\n", "for i in range(array_size) :\n", "\tprint \"Integer #%d = %d.\"%(i+1,array[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter up to 20 integers, using -9999 as a trailer:\n", "3 75 -34 6 -1 2 41 0 90 -9999 \n", "\n", "You have entered 9 integers\n", "Integer #1 = 3.\n", "Integer #2 = 75.\n", "Integer #3 = -34.\n", "Integer #4 = 6.\n", "Integer #5 = -1.\n", "Integer #6 = 2.\n", "Integer #7 = 41.\n", "Integer #8 = 0.\n", "Integer #9 = 90.\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-5, Page Number: 233" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Caculating the average grade for a class\n", "\n", "# Macro definition\n", "\n", "MAX_GRADES = 20 \t# The class may have up to 20 students\n", "TRAILER = -9999\t\t# -9999 marks the end of the grade list\n", "\n", "# Read in the grades\n", "\n", "print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n", "grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n", "for num_grades in range(MAX_GRADES) :\n", "\tprint grades[num_grades]\n", "\tif grades[num_grades] == -9999 :\n", "\t\tbreak\n", "\n", "print \"\\nYou have entered %d grades.\"%num_grades\n", "\n", "# Find the average\n", "sum = 0.0 \n", "for i in range(num_grades) :\n", "\tsum += grades[i]\n", "\n", "print \"\\nThe average grade is %f.\"%(sum/num_grades)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter up to 20 grades, using -9999 as a trailer:\n", "41\n", "92\n", "15\n", "65\n", "0\n", "78\n", "81\n", "96\n", "-9999\n", "\n", "You have entered 8 grades.\n", "\n", "The average grade is 58.500000.\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-6, Page Number: 235" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Determining which grades in a class are above the average\n", "\n", "# Macro definition\n", "\n", "MAX_GRADES = 20 \t# The class may have up to 20 students\n", "TRAILER = -9999\t\t# -9999 marks the end of the grade list\n", "\n", "# Read in the grades\n", "\n", "print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n", "grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n", "for num_grades in range(MAX_GRADES) :\n", "\tprint grades[num_grades]\n", "\tif grades[num_grades] == -9999 :\n", "\t\tbreak\n", "\n", "print \"\\nYou have entered %d grades.\"%num_grades\n", "\n", "# Find the average\n", "sum = 0.0 \n", "for i in range(num_grades) :\n", "\tsum += grades[i]\n", "\n", "avg_grade = sum/num_grades\n", "print \"\\nThe average grade is %f.\"%avg_grade\n", "\n", "# Find who got above average\n", "\n", "print \"\\nThe following grades were above average:\"\n", "for i in range(num_grades) :\n", "\tif grades[i] > avg_grade :\n", "\t\tprint \"%f (student #%d)\"%(grades[i],i+1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter up to 20 grades, using -9999 as a trailer:\n", "41\n", "92\n", "15\n", "65\n", "0\n", "78\n", "81\n", "96\n", "-9999\n", "\n", "You have entered 8 grades.\n", "\n", "The average grade is 58.500000.\n", "\n", "The following grades were above average:\n", "92.000000 (student #2)\n", "65.000000 (student #4)\n", "78.000000 (student #6)\n", "81.000000 (student #7)\n", "96.000000 (student #8)\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-7, Page Number 237" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Reversing an array\n", "\n", "# Macro definition\n", "ARRAY_SIZE = 10 \t# The array has 10 elements \n", "\n", "# Read in the array\n", "\n", "print \"Please enter %d integers:\"%ARRAY_SIZE\n", "array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]\n", "for i in range(10) :\n", "\tprint array[i],\n", "\n", "# Echo the array\n", "\n", "print \"\\n\\nThe original array was:\"\n", "for i in range(10) :\n", "\tprint array[i],\n", "\n", "# Print the array in reverse\n", "\n", "print \"\\n\\nThe reversed array is:\"\n", "for i in range(ARRAY_SIZE-1,-1,-1) :\n", "\tprint array[i]," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter 10 integers:\n", "10 42 7 0 923 13 -5 6 19 3 \n", "\n", "The original array was:\n", "10 42 7 0 923 13 -5 6 19 3 \n", "\n", "The reversed array is:\n", "3 19 6 -5 13 923 0 7 42 10\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-8, Page Number: 240" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "# Reversing a character string\n", "\n", "# Macro definition\n", "MAX_STRING_SIZE = 10\n", "\n", "# Read in the word\n", "\n", "print \"Please enter a word of up to %d letters:\"%MAX_STRING_SIZE\n", "word = \"bingo\"\n", "print word\n", "\n", "# Echo the word \n", "\n", "print \"\\nThe word is %s.\"%word\n", "\n", "# Find thw word length\n", "string_size = 0\n", "for i in word :\t\t\t# To iterate over the entire string\n", "\tstring_size += 1\n", "\n", "# Print the word in reverse\n", "\n", "print \"\\nThe reversed word is \",\n", "for i in range(string_size-1,-1,-1) :\n", "\tsys.stdout.write(\"%c\"%word[i]) # For printing in the same line without implicit whitespaces\n", "\n", "print " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter a word of up to 10 letters:\n", "bingo\n", "\n", "The word is bingo.\n", "\n", "The reversed word is ognib\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-9, Page Number: 242" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Calculating the minimum and maximum grade in a class\n", "\n", "# Macro definition\n", "\n", "MAX_GRADES = 20 \t# The class may have up to 20 students\n", "TRAILER = -9999\t\t# -9999 marks the end of the grade list\n", "\n", "# Read in the grades\n", "\n", "print \"Please enter up to %d grades, using %d as a trailer:\"%(MAX_GRADES,TRAILER)\n", "grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]\n", "for num_grades in range(MAX_GRADES) :\n", "\tprint grades[num_grades]\n", "\tif grades[num_grades] == -9999 :\n", "\t\tbreak\n", "\n", "print \"\\nYou have entered %d grades.\"%num_grades\n", "\n", "# Find the minimum grade\n", "\n", "min = grades[0]\n", "for i in range(num_grades) :\n", "\tif grades[i] < min :\n", "\t\tmin = grades[i]\n", "\n", "print \"\\nThe lowest grade is %f\"%min\n", "\n", "# Find the maximum grade\n", "\n", "max = grades[0]\n", "for i in range(num_grades) :\n", "\tif grades[i] > min :\n", "\t\tmax = grades[i]\n", "\n", "print \"\\nThe lowest grade is %f\"%max\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter up to 20 grades, using -9999 as a trailer:\n", "41\n", "92\n", "15\n", "65\n", "0\n", "78\n", "81\n", "96\n", "-9999\n", "\n", "You have entered 8 grades.\n", "\n", "The lowest grade is 0.000000\n", "\n", "The lowest grade is 96.000000\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-10, Page Number: 243" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Sorting an array\n", "\n", "# Macro definition\n", "MAX_ARRAY_SIZE = 20 \t# The array may have up to 20 elements\n", "\n", "array_size = 10\n", "print \"How many numbers?\",\n", "print array_size\n", "\n", "while array_size < 1 or array_size > MAX_ARRAY_SIZE\t:\n", "\tprint \"How many numbers?\",\n", "\tprint array_size\n", "\n", "# Read in the array\n", "\n", "print \"Please enter the integers:\"\n", "array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]\n", "for i in range(array_size) :\n", "\tprint array[i],\n", "\n", "# Echo the array\n", "\n", "print \"\\n\\nThe original array is:\"\n", "for i in range(array_size) :\n", "\tprint array[i],\n", "\n", "# Sort the array\n", "\n", "for i in range(array_size-1) :\n", "\tfor j in range(i+1, array_size) :\n", "\t\tif array[i] > array[j] :\n", "\t\t\ttemp = array[i]\n", "\t\t\tarray[i] = array[j]\n", "\t\t\tarray[j] = temp\n", "\n", "# Printing the sorted array\n", "\n", "print \"\\n\\nThe sorted array is\"\n", "for i in range(array_size) :\n", "\tprint array[i]," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many numbers? 10\n", "Please enter the integers:\n", "10 42 7 0 923 13 -5 6 19 3 \n", "\n", "The original array is:\n", "10 42 7 0 923 13 -5 6 19 3 \n", "\n", "The sorted array is\n", "-5 0 3 6 7 10 13 19 42 923\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-11, Page Number: 247" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Finding the median of an array\n", "\n", "# Macro definition\n", "MAX_ARRAY_SIZE = 20 \t# The array may have up to 20 elements\n", "\n", "array_size = 0\n", "array = []\n", "\n", "def get_size()\t:\n", "\t\"\"\" reads the size of the array (until a valid value is entered) \"\"\"\n", "\tglobal array_size\n", "\tarray_size = 10\n", "\tprint \"How many numbers?\",\n", "\tprint array_size\n", "\twhile array_size < 1 or array_size > MAX_ARRAY_SIZE\t:\n", "\t\tprint \"How many numbers?\",\n", "\t\tprint array_size\n", "\n", "\n", "def read_array() :\n", "\t\"\"\" read in the array \"\"\"\n", "\tglobal array\n", "\tprint \"Please enter the integers:\"\n", "\tarray = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3 ]\n", "\tfor i in range(array_size) :\n", "\t\tprint array[i],\n", "\n", "def write_array() :\n", "\t\"\"\" write out the array \"\"\"\n", "\tfor i in range(array_size) :\n", "\t\tprint array[i],\n", "\n", "def sort_array() :\n", "\t\"\"\" sort the array \"\"\"\n", "\tglobal array\n", "\tfor i in range(array_size-1) :\n", "\t\tfor j in range(i+1, array_size) :\n", "\t\t\tif array[i] > array[j] :\n", "\t\t\t\ttemp = array[i]\n", "\t\t\t\tarray[i] = array[j]\n", "\t\t\t\tarray[j] = temp\n", "\n", "def even(n) :\n", "\t\"\"\" returns non-zero(true) if n is even else returns zero(false) \"\"\"\n", "\treturn ((n/2)*2 == n)\n", "\n", "get_size()\t\t# Read in the size of the array\n", "\n", "read_array()\t# Read in the array\n", "\n", "print \"\\n\\nThe original array is:\"\n", "write_array()\t# Echo the array\n", "\n", "sort_array()\t# Sort the array\n", "\n", "print \"\\n\\nThe sorted array is:\"\n", "write_array()\t# Print the sorted array\n", "\n", "print \"\\n\\nThe median is:\"\n", "# The equivalent of tertiary conditional operator in python is a if test else b\n", "print \"%.1f\"%( ((array[ array_size / 2 -1] + array[ array_size / 2 ]) / 2.0 ) if even(array_size) else float(array[ array_size / 2 ]) ) " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many numbers? 10\n", "Please enter the integers:\n", "10 42 7 0 923 13 -5 6 19 3 \n", "\n", "The original array is:\n", "10 42 7 0 923 13 -5 6 19 3 \n", "\n", "The sorted array is:\n", "-5 0 3 6 7 10 13 19 42 923 \n", "\n", "The median is:\n", "8.5\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-12, Page Number: 251" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Tally how many of each number within a range are entered\n", "\n", "# Macro definition\n", "RANGE_LOW = 1\n", "RANGE_HIGH = 10\t\t\t# Tallies numbers between RANGE_LOW and RANGE_HIGH\n", "TRAILER = -1 \t\t\t# TRAILER marks end of data\n", "\n", "inputfeed = [ 3, 5, 1, 10, 8, 5, 10, 6, 2, 11, 4, 9, -2, 3, 2, 9, -1 ]\t\t# additional array for the sake of providing input\n", "\n", "tally = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\t\t# empty array\n", "# Input numbers\n", "print \"Please enter a number between %d and %d (%d to quit): \"%(RANGE_LOW,RANGE_HIGH,TRAILER),\n", "num = inputfeed[0]\n", "print num\n", "\n", "i = 1\n", "while num != TRAILER :\n", "\tif num < RANGE_LOW or num > RANGE_HIGH : \t\t# Out of range\n", "\t\tprint \"%d is not between %d and %d.\"%(num,RANGE_LOW,RANGE_HIGH)\n", "\telse :\n", "\t\ttally[ num - RANGE_LOW] += 1\t\n", "\tprint \"\\nEnter another number: \",\n", "\tnum = inputfeed[i]\n", "\tprint num\n", "\ti += 1 \t\t\t# to iterate over the input array\n", "\n", "# Print totals\n", "\n", "print \"\\n------------\\n\\nYou entered:\"\n", "for i in range(RANGE_HIGH - RANGE_LOW + 1) :\n", "\tprint \"%2d %2d's\"%(tally[i],i + RANGE_LOW)\n", "\n", "print \"\\nThank you.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter a number between 1 and 10 (-1 to quit): 3\n", "\n", "Enter another number: 5\n", "\n", "Enter another number: 1\n", "\n", "Enter another number: 10\n", "\n", "Enter another number: 8\n", "\n", "Enter another number: 5\n", "\n", "Enter another number: 10\n", "\n", "Enter another number: 6\n", "\n", "Enter another number: 2\n", "\n", "Enter another number: 11\n", "11 is not between 1 and 10.\n", "\n", "Enter another number: 4\n", "\n", "Enter another number: 9\n", "\n", "Enter another number: -2\n", "-2 is not between 1 and 10.\n", "\n", "Enter another number: 3\n", "\n", "Enter another number: 2\n", "\n", "Enter another number: 9\n", "\n", "Enter another number: -1\n", "\n", "------------\n", "\n", "You entered:\n", " 1 1's\n", " 2 2's\n", " 2 3's\n", " 1 4's\n", " 2 5's\n", " 1 6's\n", " 0 7's\n", " 1 8's\n", " 2 9's\n", " 2 10's\n", "\n", "Thank you.\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10-13, Page Number: 255" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to report car prices. User enters car make by number and car year. \n", "# Program reports price. All data is fictitious\n", "\n", "# 2-D array (4 X 12 ) of prices : row = make , column = year\n", "price_list = [\t[ 4775, 4980, 5222, 5305, 5483, 5547, 5596, 5713, 5842, 5903, 6043, 6230 ], # Volkswagen\n", "\t\t\t\t[ 4853, 5140, 5413, 5590, 5723, 5848, 5762, 5944, 6104, 6255, 6370, 6526 ],\t# Chevy\n", "\t\t\t\t[ 5627, 5772, 5973, 6210, 6539, 6720, 6792, 6930, 7054, 7202, 7355, 7562 ], # Cadillac\n", "\t\t\t\t[ 1576, 1738, 1970, 2151, 2205, 2280, 2442, 2580, 2814, 1953, 3078, 3201 ],\t# Honda\n", "\t\t\t]\t\n", "\n", "def get_make() : \n", "\t\"\"\" read in and return the number (make) of the desired car \"\"\"\n", "\t\n", "\t# Print the choice numbers and names\n", "\tfor i in range(1,5) :\n", "\t\tprint \"%d=\"%i,\n", "\t\tprint_make(i)\n", "\t\tprint \",\",\n", "\tprint \"0=END\"\n", "\n", "\t# Read in the selection by number\n", "\tprint \" Which car do you want? \",\n", "\tmake = 2\n", "\tprint make\n", "\twhile make < 0 or make > 4 :\n", "\t\tprint \" Which car do you want? \",\n", "\t\tmake = 2\n", "\t\tprint make\n", "\n", "\treturn make\n", "\n", "def get_year() :\t\n", "\t\"\"\" read in and return the year of the desired car \"\"\"\n", "\n", "\tprint \" What year (1975-1986)?\",\n", "\tyear = 1980\n", "\tprint year\n", "\twhile year < 1975 and year > 1986 :\n", "\t\tprint \" What year (1975-1986)?\",\n", "\t\tyear = 1980\n", "\t\tprint year\n", "\n", "\treturn year\n", "\n", "def price (car,yr) :\n", "\t\"\"\" Use \"car\" and \"year\" to generate an index into \"price_list\" and return the corresponding entry \"\"\"\n", "\treturn price_list[ car - 1 ][ yr - 1975]\n", "\n", "def print_make(car) :\n", "\t\"\"\" print the make of \"car\" according to its value \"\"\"\n", "\tif car == 1 : \n", "\t\tprint \"Volkswagen\",\n", "\telif car == 2 :\n", "\t\tprint \"Chevy\",\n", "\telif car == 3 :\n", "\t\tprint \"Cadillac\",\n", "\telse :\n", "\t\tprint \"Honda\",\n", "\n", "make = get_make()\n", "\n", "while make != 0 :\t\t# O indicates end of input\n", "\tyear = get_year()\n", "\tprint \"The price of a %d\"%year,\n", "\tprint_make(make)\n", "\tprint \" is $%d.\\n\"%price(make,year)\n", "\tmake = 0 # To terminate the loop\n", "\n", "print \"\\nThank you for buying all those cars!\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1= Volkswagen , 2= Chevy , 3= Cadillac , 4= Honda , 0=END\n", " Which car do you want? 2\n", " What year (1975-1986)? 1980\n", "The price of a 1980 Chevy is $5848.\n", "\n", "\n", "Thank you for buying all those cars!\n" ] } ], "prompt_number": 13 } ], "metadata": {} } ] }