Control flow: Loops
嚜澧ontrol flow: Loops
Rob Thompson
UW CSE 160
Winter 2021
1
Exercise: Convert temperatures
? Make a temperature conversion chart, from Fahrenheit to
Centigrade, for these Fahrenheit values: 30, 40, 50, 60, 70
? Output (approximate):
30 -1.11
40 4.44
50 10.0
60 15.56
70 21.11
All done
? Hint: cent = (fahr - 32) / 9.0 * 5
2
Temperature conversion chart
One possible Python program that solves this:
fahr = 30
cent = (fahr - 32)
print(fahr, cent)
fahr = 40
cent = (fahr - 32)
print(fahr, cent)
fahr = 50
cent = (fahr - 32)
print(fahr, cent)
fahr = 60
cent = (fahr - 32)
print(fahr, cent)
fahr = 70
cent = (fahr - 32)
print(fahr, cent)
print("All done")
See in python tutor
/ 9.0 * 5
/ 9.0 * 5
/ 9.0 * 5
/ 9.0 * 5
/ 9.0 * 5
Output:
30 -1.11
40 4.44
50 10.0
60 15.56
70 21.11
All done
3
Copy and Paste Problems
? Can take a long time (luckily this list only had 5
values in it!)
? Error prone
? What about #
每 Modifications: I decide I want to change the output
format?
每 Bugs: I made a mistake in the formula?
每 Readability: Is it obvious to a human reader that all 5
chunks of code are identical without looking carefully?
4
For each fahr, do ※this§
? Where ※this§ is:
cent = (fahr - 32) / 9.0 * 5
print(fahr, cent)
? Would be nice if we could write ※this§ just once
每 Easier to modify
每 Easier to fix bugs
每 Easier for a human to read
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- chapter 4 loops
- exploring data using python 3 charles r severance
- intermediate python read the docs
- control flow loops
- python dictionaries university of michigan
- lecture 16 cornell university
- programming principles in python csci 503
- python chapter 3 repetition 3 1 while loop
- python programming exercises 4
- programming for gcse topic 6 1 lists arrays and for loop