-
Notifications
You must be signed in to change notification settings - Fork 18
/
Exercise1.py
477 lines (420 loc) · 12.3 KB
/
Exercise1.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# -Exercises1.py *- coding: utf-8 -*-
"""
Student needs:
Exercises1.py
We will mainly use the following window panes: IPython Console, Editor,
File Explorer, and Object Inspector.
#%% breaks up the Editor document into cells. The green triangle in the tool
bar executes the entire file (after saving it), Ctrl-Enter (Command-Return on a
Mac) executes only the cell that the cursor is in (but does not save).
Instructions on changing working directory in Spyder: At the top on the right
you will see a path, the working directory. To its right is a yellow file
folder. Click it and you can change the working directory. When you do, you
can click the icon to the right of that and set that path as the current
console's new working directory. Then all the panes: Editor, IPython Console,
and File Explorer are pointed to this current working directory.
"""
"""
Quick look at arithmetic operations
+, -, *, **, /, //, %
These add, subtraction, multiple, exponentiate, divide, integer divide (drops
fractional part), computes remainder on division for integers.
Try some examples interactively in IPython window on lower right.
"""
#%%
def hello():
""" prints hello, world """
print("Hello, world!")
#%%
def areacircle(radius):
""" Computes the area of a circle of the given radius """
area = 3.14*radius**2
print("The area of a circle of radius",radius,"is", area)
#%%
"""
Exercise:
Write a function 'def areatriangle(b,h)' to compute the area
of a triangle: formula is area = .5*b*h.
Output should look like:
The area of a triangle of base 3 and height 5 is 7.5
You can test your function by executing the following code:
"""
#%%
# The following will test areatriangle()
areatriangle(3,5)
areatriangle(2,20)
#%%
"""
Solution:
"""
#%%
def areatriangle(b,h):
area = (b*h)/2
print("The area of a traingle with base",b,"and height",h,"is",area)
#%%
"""
End solution
"""
"""
To make a string we may use ' or ". Either works equally well. But if the
string contains one, we need to use the other:
"""
#%%
name = "His name is Conan O'Brien"
cat = 'My cat is named "Butters"'
print(name)
print(cat)
#%%
"""
If you need both a ' and a " in your string, you can use the escape
character \ which tells Python that the following character is to be taken
as the literal character and is not a quote to delimit the string. See it
in action escaping the " below:
"""
#%%
both = "My cat's name is \"Butters\""
print(both)
#%%
def fahrenheit_to_celsius(temp):
""" Converts Fahrenheit temperature to Celsius.
Formula is 5/9 of temp minus 32 """
# Note that this line is not executed
# end='' keeps print from starting a new line.
newTemp = 5*(temp-32)/9
print("The Fahrenheit temperature",temp,"is equivalent to",newTemp,end='')
print(" degrees Celsius")
#%%
"""
Exercise:
Write a function 'def celsius_to_fahrenheit(temp)' to convert Celsius
to Fahrentheit temperature. The formula is (9/5) times temp plus 32.
Print the output in the form:
The Celsius temperature 50.0 is equivalent to 122.0 degrees Fahrenheit.
"""
#%%
# The following will test the above function
celsius_to_fahrenheit(100)
celsius_to_fahrenheit(0)
celsius_to_fahrenheit(50.)
#%%
"""
Solution:
"""
#%%
def celsius_to_fahrenheit(temp):
newTemp = (9*temp/5)+32
print("The Celsius temperature",temp,"is equivalent to",newTemp,end='')
print(" degrees fahrenheit")
#%%
"""
End solution
"""
#%%
def name():
""" Input first and last name, combine to one string and print """
fname = input("Enter your first name: ")
lname = input("Enter your last name: ")
fullname = fname + " " + lname
print("Your name is:", fullname)
#%%
"""
Exercise:
Extend the name function written in class to include the city and state.
That is, ask two more questions to get the city and the state you live in.
Print where you are from on a new line. Put the customary comma between
city and state. to save time, here is the starting function.
Your run should look like the following (even if this is not the customary
way in your country):
Enter your first name: Bill
Enter your last name: Boyd
Enter the city you live in: Middletown
Enter the state you live in: CT
Your name is: Bill Boyd
You live in: Middletown, CT
"""
"""
Solution:
"""
#%%
def name():
""" Input first and last name, combine to one string and print
Also, input the city and state and print."""
fname = input("Enter your first name: ")
lname = input("Enter your last name: ")
fullname = fname + " " + lname
city = input("Enter the city you live in: ")
state = input("Enter the state you live in: ")
fullLocation = city + ", " + state
print("Your name is:", fullname)
print("You live in:",fullLocation)
#%%
"""
End solution
"""
#%%
def if_statement():
""" Three slightly difference versions of if: if, if-else, if-elif-else"""
x = 5
y = 0
z = 0
if x > 0:
print("x is positive")
if y > 0:
print("y is positive")
else:
print("y is not positive")
# elif can be repeated as often as necessary
if z > 0:
print("z is positive")
elif z < 0:
print("z is negative")
else:
print("z must be 0")
#%%
"""
Python uses '=' for assignment and '==' for testing equality. Also '!=' is
used to test for non-equality. Try these examples:
"""
#%%
x = 5
y = 5
z = 6
#%%
"""
Now we try to following:
"""
print("x is equal to y: ", x == y)
print("x is not equal to y: ", x != y)
print("x is equal to z: ", x == z)
print("x is not equal to z: ", x != z)
#%%
"""
The following function uses an 'if' statement. Note that the indention marks
the scope of the 'if', 'elif', 'else' actions.
"""
def area(type_, x):
""" Computes the area of a square or circle.
type_ must be the string "circle or the string "square"
We use type_ here, because type is a Python keyword. """
if type_ == "circle":
area = 3.14*x**2
print(area)
elif type_ == "square":
area = x**2
print(area)
else:
print("I don't know that one.")
#%%
"""
Exercise:
Write a function absolutevalue(num) that computes the absolute value of
a number. You will need to use an 'if' statement. Remember if a number is
less than zero then you must multiply by -1 to make it greater than zero.
Give output in the form:
The absolute value of -5 is 5
"""
#%%
# Test runs
absolutevalue(5)
absolutevalue(-5)
absolutevalue(4-4)
#%%
"""
Solution:
"""
#%%
def absolutevalue(num):
if num < 0:
absnum =num*-1
else:
absnum = num
print("The absolute value of", num,"is",absnum)
#%%
"""
End solution
"""
"""
Example: The next three examples work with the 'input' statement and point out
some of the things that you might need to be aware of in using one. It also
shows how to use the 'print' statement without having a new line started at
the end of that statement by using an 'end' argument in it.
"""
#%%
def fahrenheit_to_celsius1():
""" BAD. Does not check input before using it.
Input from keyboard, which is always a string and must often be
converted to an int or float.
Converts Fahrenheit temp to Celsius.
"""
temp_str = input("Enter a Fahrentheit temperature: ")
temp = int(temp_str)
newTemp = 5*(temp-32)/9
print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
print(newTemp,"degrees Celsius")
#%%
"""
Test the program above by entering a temperature such as 212. Also check what
happens if you simply press enter.
"""
#%%
def fahrenheit_to_celsius2():
""" IMPROVED. Does some checking of input before using it.
Input from keyboard, which is always a string and must often be
converted to an int or float.
Converts Fahrenheit temp to Celsius.
Uses 'if' to make sure an entry was made.
"""
temp_str = input("Enter a Fahrenheit temperature: ")
if temp_str:
temp = int(temp_str)
newTemp = 5*(temp-32)/9
print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
print(newTemp,"degrees Celsius")
#%%
"""
Test the program above by entering the temperature 212 and also by simply
pressing 'Enter' or 'Return' key. Note the improvement. Now try entering 'a'.
"""
#%%
def fahrenheit_to_celsius3():
""" MORE IMPROVED. Does even more checking of input before using it.
Input from keyboard, which is always a string and must often be
converted to an int or float.
Converts Fahrenheit temp to Celsius.
Uses if to check whether input is a number and then uses .isdigit() method
of strings to check whether input is made of of digits.
"""
temp_str = input("Enter a Fahrentheit temperature: ")
if temp_str:
if temp_str.isdigit():
temp = int(temp_str)
newTemp = 5*(temp-32)/9
print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
print(newTemp,"degrees Celsius")
else:
print("You must enter a number. Bye")
#%%
"""
Test the program above by entering the temperature 212, by simply pressing
'Enter' or 'Return' key, and by entering 'a'. Note the improvement. We will
leave the function at this point though further improvements could be made.
"""
"""
The following function uses integer division.
"""
#%%
def inches_to_feet1(inches):
""" converts inches to feet and inches """
feet = inches//12 # division by integer with fraction thrown away
extra_inches = inches - 12*feet
print(inches,"inches is",feet,"feet and",extra_inches,"inches")
#%%
"""
Exercise: Rewrite inches_to_feet1(inches) calling it inches_to_feet2(inches)
using % to compute the inches. Recall that 19 % 5 will give 4 (the remainder).
Copy and paste the original into the solution area and modify to same typing
time.
"""
"""
Solution:
"""
#%%
def inches_to_feet2(inches):
extra_inches = inches %12
feet = inches //12
print(inches,"inches is",feet,"feet and",extra_inches,"inches")
#%%
"""
End solution
"""
"""
The 'while' loop. Loops are used to repeat actions and the scope of this
repetition is indicated by the indention after the 'while' statement.
"""
#%%
def cheer():
""" Prints 2 4 6 8, who do we appreciate .... Note that everything in
the while loop is indented. The first line not indented is the first
line following the while loop. """
ct = 2
while ct <= 8:
print(ct,end=" ") # end = " " keeps from starting a new line
ct = ct + 2
print() # now we'll start a new line
print("Who do we appreciate?")
print("COURSERA!")
#%%
"""
Exercise:
Write a function count_down() that starts at 10 and counts down to rocket
launch. It's output should be 10 9 8 7 6 5 4 3 2 1 BLASTOFF! You can make
all the numbers on the same line or different lines. Use a while loop.
"""
"""
Solution:
"""
#%%
def count_down():
count = 10
while count !=0:
print(count,end=' ')
count-=1
print("BLASTOFF!")
#%%
"""
End solution
"""
"""
The 'for' loop. This loop uses an iterator to determine how many times to go
through the loop. The iterator we use below is 'range(start, stop, step)'.
"""
#%%
def cheer2():
""" Same as cheer, but uses a for loop and range()
range uses a start number, a stop number and a step size. """
for ct in range(2,9,2):
print(ct,end=' ')
print()
print("Who do we appreciate?")
print("COURSERA!")
#%%
"""
Exercise:
Write a function countdown1() that starts at 10 and counts down to rocket
launch. It's output should be 10 9 8 7 6 5 4 3 2 1 BLASTOFF! You can make
all the numbers on the same line or different lines. Use a 'for' loop and
range(). range has a start and a stop and a step that MAY BE NEGATIVE.
"""
"""
Solution:
"""
#%%
def countdown1():
for ct in range(10,0,-1):
print(ct,end=' ')
print("BLASTOFF!)
#%%
"""
End solution
"""
#%%
"""
Some of our exercises involve finding and fixing errors in code.
Here is an example. Can you see the errors (there are two)? Note that the
editor is pointing out one line with troubles.
You can find the error by reading the example carefully, or trying to make
it work by using Shift-Enter to insert the function into IPython and reading
what error it gives or trying to run the function.
"""
#%%
def favorite():
my_toy = input("What is my favorite toy? ")
print("Your favorite toy is", my_toy)
#%%
"""
My solution:
"""
#%%
"""
end solution
"""