summaryrefslogtreecommitdiff
path: root/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb
diff options
context:
space:
mode:
authordebashisdeb2014-06-20 15:42:42 +0530
committerdebashisdeb2014-06-20 15:42:42 +0530
commit83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch)
treef54eab21dd3d725d64a495fcd47c00d37abed004 /Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb
parenta78126bbe4443e9526a64df9d8245c4af8843044 (diff)
downloadPython-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip
removing problem statements
Diffstat (limited to 'Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb')
-rw-r--r--Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb601
1 files changed, 510 insertions, 91 deletions
diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb
index 8a41802e..9a59031c 100644
--- a/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb
+++ b/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch3"
+ "name": "",
+ "signature": "sha256:11b7a1493680a6b7a0a36e37cb0fb0b3cfce2dde29e20a1b991383c6bbfc7afe"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,31 +11,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.1 Testing for Divisibility\nThis program tests if one positive integer is not divisible by another:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "66\n"
+ "text": [
+ "66\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "66 is not divisible by 7\n"
+ "text": [
+ "66 is not divisible by 7\n"
+ ]
}
],
"prompt_number": 1
@@ -42,31 +59,48 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.2 Testing for Divisibility Again\nThis program is the same as the program in Example 3.1 except that the if statement has been replaced\nby an if..else statement:\n'''\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)\nelse:\n print \"%d is divisible by %d\" %(n,d)\n",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "56\n"
+ "text": [
+ "56\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "7\n"
+ "text": [
+ "7\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "56 is divisible by 7\n"
+ "text": [
+ "56 is divisible by 7\n"
+ ]
}
],
"prompt_number": 2
@@ -74,31 +108,51 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.3 The Minimum of Two Integers\nThis program prints the minimum of the two integers entered:\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\n\nif (m < n):\n print \"%d is the minimum.\" %m\nelse:\n print \"%d is the minimum.\" %n\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "55 is the minimum.\n"
+ "text": [
+ "55 is the minimum.\n"
+ ]
}
],
"prompt_number": 3
@@ -106,7 +160,15 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.4 A Common Programming Error\nThis program is erroneous:\n'''\nprint \"Enter an integer: \"\nn = int(raw_input())\nif (n = 22):\n print \"%d = 22\" %n\nelse: \n print \"%d != 22\" %n",
+ "input": [
+ "\n",
+ "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": [
@@ -124,37 +186,61 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.5 The Minimum of Three Integers\nThis program is similar to the one in Example 3.3 except that it applies to three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nm=n1\n# now min <= n1\nif (n2 < m):\n m = n2 # now min <= n1 and min <= n2\nif (n3 < m):\n m = n3 # now min <= n1, min <= n2, and min <= n3\nprint \"Their minimum is %d\" % m",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Their minimum is 33\n"
+ "text": [
+ "Their minimum is 33\n"
+ ]
}
],
"prompt_number": 5
@@ -162,31 +248,53 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.6 A Statement Block within an if Statement\nThis program inputs two integers and then outputs them in increasing order:\n'''\n\nprint \"Enter two integers: \"\nx = int(raw_input())\ny = int(raw_input())\n\nif (x > y):\n temp=x\n x = y\n y = temp\n \n\nprint \"%d <= %d\" %(x,y)",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "66\n"
+ "text": [
+ "66\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "44 <= 66\n"
+ "text": [
+ "44 <= 66\n"
+ ]
}
],
"prompt_number": 6
@@ -194,25 +302,52 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.7 Using Blocks to Limit Scope\nThis program uses the same name n for three different variables:\n'''\nn=44\nprint \"n = %d\" % n \nif True:\n # scope extends over 4 lines\n print \"Enter an integer: \"\n n = int(raw_input())\n print \"n = %d\" % n \n\nif True:\n print \"n = %d\" % n \n # the n that was declared first\nif True:\n print \"n = %d\" % n \n\nprint \"n = %d\" % n ",
+ "input": [
+ "\n",
+ "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\nEnter an integer: \n"
+ "text": [
+ "n = 44\n",
+ "Enter an integer: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "n = 77\nn = 77\nn = 77\nn = 77\n"
+ "text": [
+ "n = 77\n",
+ "n = 77\n",
+ "n = 77\n",
+ "n = 77\n"
+ ]
}
],
"prompt_number": 7
@@ -220,37 +355,60 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.8 Using Compound Conditions\nThis program has the same effect as the one in Example 3.5 on page 39. This version uses compound\nconditions to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 <= n2 and n1 <= n3):\n print \"Their minimum is %d\" % n1\n \nif (n2 <= n1 and n2 <= n3):\n print \"Their minimum is %d \" % n2 \nif (n3 <= n1 and n3 <= n2):\n print \"Their minimum is %d\" % n3 \n",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Their minimum is 33 \n"
+ "text": [
+ "Their minimum is 33 \n"
+ ]
}
],
"prompt_number": 8
@@ -258,25 +416,41 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.9 User-Friendly Input\nThis program allows the user to input either a 'Y' or a 'y' for 'yes':\n'''\n\nprint \"Are you enrolled (y/n): \"\nans = raw_input()\nif (ans == 'Y' or ans == 'y'):\n print \"You are enrolled.\\n\"\nelse: \n print \"You are not enrolled.\\n\"\n",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Are you enrolled (y/n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "y\n"
+ "text": [
+ "y\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "You are enrolled.\n\n"
+ "text": [
+ "You are enrolled.\n",
+ "\n"
+ ]
}
],
"prompt_number": 9
@@ -284,31 +458,50 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.10 Short-Circuiting\nThis program tests integer divisibility:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0 and n%d == 0): \n print \"%d divides %d\" %(d,n)\nelse:\n print \"%d does not divide %d\"% (d,n)",
+ "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"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6\n"
+ "text": [
+ "6\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "6 does not divide 33\n"
+ "text": [
+ "6 does not divide 33\n"
+ ]
}
],
"prompt_number": 10
@@ -316,32 +509,49 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.11 Another Logical Error\nThis program is erroneous:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nif (n1 >= n2 >= n3):\n print \"max = x\"",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "0\n"
+ "text": [
+ "0\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "1\n"
+ "text": [
+ "1\n"
+ ]
}
],
"prompt_number": 11
@@ -349,31 +559,53 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.12 Nesting Selection Statements\nThis program has the same effect as the one in Example 3.10 on page 42:\n'''\nprint \"Enter two positive integers: \"\nn = int(raw_input())\nd = int(raw_input())\n\nif (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)\nelse:\n print '%d does not divide %d '%(d,n)",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter two positive integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "44\n"
+ "text": [
+ "44\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "44 does not divide 55\n"
+ "text": [
+ "44 does not divide 55\n"
+ ]
}
],
"prompt_number": 12
@@ -381,37 +613,63 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.13 Using Nested Selection Statements\nThis program has the same effect as those in Example 3.5 on page 39 and Example 3.8 on page 41.\nThis version uses nested if..else statements to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 < n2):\n if (n1 < n3):\n print \"Their minimum is : %d\" % n1\n else:\n print \"Their minimum is : %d\" % n3\nelse: # n1 >= n2\n if (n2 < n3):\n print \"Their minimum is : %d\" % n2\n else:\n print \"Their minimum is %d\" % n3",
+ "input": [
+ "\n",
+ "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"
+ "text": [
+ "Enter three integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "77\n"
+ "text": [
+ "77\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Their minimum is : 33\n"
+ "text": [
+ "Their minimum is : 33\n"
+ ]
}
],
"prompt_number": 13
@@ -419,58 +677,117 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.14 A Guessing Game\nThis program finds a number that the user selects from 1 to 8:\n'''\n\nprint \"Pick a number from 1 to 8.\" \nanswer = int(raw_input())\nprint \"Is it less than 5? (y|n): \"\nanswer = raw_input()\nif (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.\"\nelse: # 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.\"",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Pick a number from 1 to 8.\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "6\n"
+ "text": [
+ "6\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Is it less than 5? (y|n): \n"
+ "text": [
+ "Is it less than 5? (y|n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "n\n"
+ "text": [
+ "n\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Is it less than 7? (y|n): \n"
+ "text": [
+ "Is it less than 7? (y|n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "y\n"
+ "text": [
+ "y\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Is it less than 6? (y|n): \n"
+ "text": [
+ "Is it less than 6? (y|n): \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "n\n"
+ "text": [
+ "n\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your number is 6.\n"
+ "text": [
+ "Your number is 6.\n"
+ ]
}
],
"prompt_number": 14
@@ -478,7 +795,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.15 Using the else if Construct for Parallel Alternatives\nThis program requests the users language and then prints a greeting in that language:\n'''\nlanguage = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n\nif (language == 'e'): \n print \"Welcome to ProjectEuclid.\"\nelif (language == 'f'):\n print \"Bon jour, ProjectEuclid.\"\nelif (language == 'g'):\n print \"Guten tag, ProjectEuclid.\"\nelif (language == 'i'):\n print \"Bon giorno, ProjectEuclid.\"\nelif (language == 'r'):\n print \"Dobre utre, ProjectEuclid.\"\nelse:\n print \"Sorry; we don't speak your language.\"\n",
+ "input": [
+ "\n",
+ "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": [
@@ -486,12 +819,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n"
+ "text": [
+ "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Bon giorno, ProjectEuclid.\n"
+ "text": [
+ "Bon giorno, ProjectEuclid.\n"
+ ]
}
],
"prompt_number": 15
@@ -499,7 +836,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.16 Using the else if Construct to Select a Range of Scores\nThis program converts a test score into its equivalent letter grade:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n",
+ "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": [
@@ -507,12 +860,16 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your test score: 83\n"
+ "text": [
+ "Enter your test score: 83\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your grade is a B.\n"
+ "text": [
+ "Your grade is a B.\n"
+ ]
}
],
"prompt_number": 16
@@ -520,7 +877,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.17 Using a switch Statement to Select a Range of Scores\nThis program has the same effect as the one in Example 3.16:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"",
+ "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",
+ "\n",
+ "print \"Goodbye.\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -528,12 +903,17 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your test score: 83\n"
+ "text": [
+ "Enter your test score: 83\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your grade is a B.\nGoodbye.\n"
+ "text": [
+ "Your grade is a B.\n",
+ "Goodbye.\n"
+ ]
}
],
"prompt_number": 17
@@ -541,7 +921,25 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.18 An Erroneous Fall-through in a switch Statement\nThis program was intended to have the same effect as the one in Example 3.17. But with\nstatements, the program execution falls through all the case statements it encounters:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"",
+ "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",
+ "\n",
+ "print \"Goodbye.\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -549,12 +947,17 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Enter your test score: 83\n"
+ "text": [
+ "Enter your test score: 83\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Your grade is a B.\nGoodbye.\n"
+ "text": [
+ "Your grade is a B.\n",
+ "Goodbye.\n"
+ ]
}
],
"prompt_number": 18
@@ -562,31 +965,47 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 3.19 Finding the Minimum Again\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint min(m,n),\nprint 'is the minimum'",
+ "input": [
+ "\n",
+ "\n",
+ "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"
+ "text": [
+ "Enter two integers: \n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "33\n"
+ "text": [
+ "33\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "55\n"
+ "text": [
+ "55\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "33 is the minimum\n"
+ "text": [
+ "33 is the minimum\n"
+ ]
}
],
"prompt_number": 19
@@ -594,7 +1013,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []