summaryrefslogtreecommitdiff
path: root/C++_By_Example/Chapter5.ipynb
blob: 445876534e79b04830a8acc9fee94d53e94c8688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
 "metadata": {
  "name": "",
  "signature": "sha256:a075bb47fc03df6e4814e3968f8a8dcc720530083267bfa7a0c1a0cea5145710"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter5, Character Input/Output and String Functions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C22INI, Page number:452"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Get input\n",
      "print \"What is your first initial?\"\n",
      "initial=raw_input()\n",
      "\n",
      "#Check if it is an alphabet\n",
      "while not initial.isalpha():\n",
      "    print \"That was not a valid initial!\"\n",
      "    print \"What is your first initial?\"\n",
      "    initial=raw_input()\n",
      "print \"Thanks\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "What is your first initial?\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "p\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Thanks\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C22GB, Page number:454"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Get input\n",
      "#ans=raw_input(\"Are you a girl or a boy (G/B)? \")  \n",
      "ans=\"b\"\n",
      "\n",
      "#convert answer to uppercase\n",
      "ans=ans.upper()   \n",
      "\n",
      "if ans=='G':\n",
      "    print \"You look pretty today!\\n\"\n",
      "else:\n",
      "    if ans=='B':\n",
      "        print \"You look handsome today!\\n\"\n",
      "    else:\n",
      "        print \"Your answer makes no sense!\\n\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "You look handsome today!\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C22GPS1, Page number:459"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#get book title\n",
      "#book=raw_input(\"What is the book title? \")  \n",
      "book=\"Mary and Her Lambs\"\n",
      "\n",
      "#print book title\n",
      "print book          \n",
      "\n",
      "print \"Thanks for the book!\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mary and Her Lambs\n",
        "Thanks for the book!\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C22ABS, Page number:463"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Get input\n",
      "#age1=input(\"\\nWhat is the first child's age? \")\n",
      "#age2=input(\"\\nWhat is the second child's age? \")\n",
      "\n",
      "age1=10\n",
      "age2=12\n",
      "\n",
      "diff=age1-age2               \n",
      "\n",
      "# abs() function determines absolute value\n",
      "diff=abs(diff)              \n",
      "\n",
      "#Result\n",
      "print \"\\nThey are \",diff,\" years apart.\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "They are  2  years apart.\n"
       ]
      }
     ],
     "prompt_number": 4
    }
   ],
   "metadata": {}
  }
 ]
}