-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter5-loops-iteration
135 lines (125 loc) · 4.83 KB
/
chapter5-loops-iteration
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
# finite, infinite, continue, break
while True: # must use'True', not 'true'; 'while', not 'While'
line = raw_input('> ') # "print this!"
if line[0] == "#": # don't print this
continue # it ends the current iteration
# and jumps back to the "top" of the loop to start the next iteration
if line == 'done':
break # it ends the current iteration and GET OUT OF the iteration
# the output is "DONE!", case sensitive
print line
print 'DONE!'
# for loop
friends = ['Joseph', 'Glenn', 'Sally']
for i in friends: # i is iteration variable in loop and friends are variables
print 'Happy New Year:', i
print 'Done!' # ctrl + x is delete line; this print is not in the body of the for loop
# counting and summing
count = 0
for i in [3,5,76,24,0,97]:
# while we do not use i in the loop
# it caused the loop body to be executed once for each of the values in the list
count = count + 1
print "Count:", count
sumup = 0
for i in [3,5,76,24,0,97]:
sumup = sumup + i
#In this loop we do use the iteration variable #Instead of simply adding one to the count as in the previous loop,
#we add the actual number, the variable sumup is the “running total of the values so far” # i is a accumulator
print "The sum of list is:", sumup
# Maximum and minimum
largest = None
print "Before:", largest
for i in [3, 41, 12, 9, 74, 15]:
if largest is None or i > largest: # see a value that is larger than the “largest so far”.
largest = i # next line no block means if clause is ends in this line
print "Loop:", i, largest # when i is 12, which is less than the largest number so far 41, so not update largest
print "After:", largest
def min(values):
smallest = None
for i in values:
if smallest is None or i < smallest:
smallest = i
return smallest # 'return' only used in def, loop used print
print min('book')
# len(), sum(), max(), min(), mid-point check
# initialize: An assignment that gives an initial value to a variable that will be updated
# Exercises 1: keeep calculating until put done; if bad input, skip and continue enter next and continue calculating
total = 0
count = 0
try:
while True:
enters = raw_input('Enter a number:')
if enters == 'done':
break
total = total + float(enters)
count = count + 1
avg = total/count
print total, count, avg
except:
print "Invalid input"
while True:
enters = raw_input('Enter a number:')
#if bad input happen twice, how to revise the code?
if enters == 'done':
break
total = total + float(enters)
count = count + 1
avg = total/count
print total, count, avg
# Exercises 2:prompts for a list of numbers
total = 0 # must use 0 to initialize when calculate ( total = total + float(i))
count = 0
mini = None # can use None to initialize if only assgining, not calculating ( mini = i)
maxi = None # Must be None(reserved), not none(variable)
try:
enters = raw_input('Enter a number:') #4,5,bad data,7,done (NO SPACE!!)
enterlist = enters. split(',') #['4', '5', 'bad data', ' 7', ' done']
for i in enterlist: #erro "unexpected indent" if this line has block
if i == 'done':
break # ignore the enters after "done" was put
total = total + float(i)
count = count + 1
if mini is None or i < mini:
mini = i
if maxi is None or i > maxi:
maxi = i
sub_enterlist = enterlist[count+1:] # [] position starting from '0'
print total, count, mini, maxi
except:
for i in sub_enterlist: # if bad data, skip and continue from next number
if i == 'done':
break # jump out of loop means starting from the line that has no block compared with for clause
total = total + float(i) # bad data can only appear once because only one except clause?
count = count + 1
if mini is 0 or i < mini:
mini = i
if maxi is 0 or i > maxi:
maxi = i
print total, count, mini, maxi # this is the line which means for loop ends (NO BLOCK)
# Exercises 3
maximum = None
minimum = None
try:
while True:
num = raw_input("Enter a number: ")
if num == "done" :
break
if maximum is None or int(num) > maximum:
maximum = int(num)
if minimum is None or int(num) < minimum:
minimum = int(num)
print "Maximum is", maximum
print "Minimum is", minimum
except:
print "Invalid input"
while True:
num = raw_input("Enter a number: ")
if num == "done" :
break
if maximum is None or int(num) > maximum:
maximum = int(num)
if minimum is None or int(num) < minimum:
minimum = int(num)
print "Maximum is", maximum
print "Minimum is", minimum