www.umbc.edu
CMSC201 Computer Science I for Majors
Lecture 09 – While Loops
- Prof. Katherine Gibson
- Prof. Jeremy Dixon
CMSC201 Computer Science I for Majors Lecture 09 While Loops Prof. - - PowerPoint PPT Presentation
CMSC201 Computer Science I for Majors Lecture 09 While Loops Prof. Katherine Gibson Prof. Jeremy Dixon www.umbc.edu Last Class We Covered Using for loops Syntax Using it to iterate over a list Using it for counting the
www.umbc.edu
www.umbc.edu
2
www.umbc.edu
www.umbc.edu
4
www.umbc.edu
www.umbc.edu
6
www.umbc.edu
7
www.umbc.edu
nums = [98, 75, 89, 100, 45, 82] total = 0 # we have to initialize total to zero for n in nums: total = total + n # so that we can use it here avg = total / len(nums) print("Your average in the class is: ", avg)
8
www.umbc.edu
9
www.umbc.edu
10
www.umbc.edu
www.umbc.edu
12
www.umbc.edu
13
www.umbc.edu
14
www.umbc.edu
15
www.umbc.edu
num = 1 # we have to initialize num while num <= 20: # so that we can use it here print(num) num = num + 1 # don't forget to update # the loop variable
16
www.umbc.edu
17
www.umbc.edu
18
www.umbc.edu
19
www.umbc.edu
20
www.umbc.edu
www.umbc.edu
22
www.umbc.edu
23
www.umbc.edu
24
www.umbc.edu
25
www.umbc.edu
26
www.umbc.edu
27
www.umbc.edu
28
www.umbc.edu
29
www.umbc.edu
30
www.umbc.edu
www.umbc.edu
32
www.umbc.edu
33
www.umbc.edu
34
www.umbc.edu
values = [] # initialize the list to be empty userVal = 1 # give loop variable an initial value while userVal != 0: userVal = int(input("Enter a number, 0 to stop: ")) if userVal != 0: # only append if it's valid values.append(userVal) # add value to the list
35
www.umbc.edu
while userVal != 0: userVal = int(input("Enter a number, 0 to stop: ")) if userVal != 0: # only append if it's valid values.append(userVal) # add value to the list
36
www.umbc.edu
37
www.umbc.edu
roster = ["Adam", "Alice", "Andy", "Ariel"]
38
www.umbc.edu
roster = ["Adam", "Alice", "Andy", "Ariel"] roster.remove("Adam") # Adam has dropped the class
39
www.umbc.edu
roster = ["Adam", "Alice", "Andy", "Ariel"] roster.remove("Adam") # Adam has dropped the class roster.remove("Bob") # Bob is not in the roster
40
www.umbc.edu
www.umbc.edu
42
www.umbc.edu
num = 0 # we have to initialize num while num <= 0: # so that we can use it here num = int(input("Enter a positive number: ")) # while loop exits because num is positive print("Thank you. The number you chose is:", num)
43
www.umbc.edu
www.umbc.edu
45
www.umbc.edu
scores = [] for i in range(10): num = 0 while num <= 0: num = int(input("Enter a positive #: ")) scores.append(num) print(scores)
46
www.umbc.edu
scores = [] for i in range(10): num = 0 while num <= 0: num = int(input("Enter a positive #: ")) scores.append(num) print(scores)
47
www.umbc.edu
www.umbc.edu
49
www.umbc.edu
twoD = [ [1,2,3], [4,5,6], [7,8,9] ]
50
www.umbc.edu
51
www.umbc.edu
52
www.umbc.edu
53
www.umbc.edu
54
www.umbc.edu
names = ['Alice', 'Bob', 'Evan']
names = [list("Alice"), list("Bob"), list("Evan")] [['A', 'l', 'i', 'c', 'e'], ['B', 'o', 'b'], ['E', 'v', 'a', 'n']]
55
www.umbc.edu
56
www.umbc.edu
57
www.umbc.edu
58
www.umbc.edu
59
www.umbc.edu
60