-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopingstatementspython.txt
96 lines (76 loc) · 1.63 KB
/
loopingstatementspython.txt
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
vowels="AEIOU"
for iter in vowels:
print("char:", iter)
int_list = [1, 2, 3, 4, 5, 6]
sum = 0
for iter in int_list:
sum += iter
print("Sum =", sum)
print("Avg =", sum/len(int_list))
for iter in range(0, 3):
print("iter: %d" % (iter))
books = ['C', 'C++', 'Java', 'Python']
for index in range(len(books)):
print('Book (%d):' % index, books[index])
###FOR_ELSE
birds = ['Belle', 'Coco', 'Juniper', 'Lilly', 'Snow']
ignoreElse = False
for theBird in birds:
print(theBird )
if ignoreElse and theBird is 'Snow':
break
else:
print("No birds left.")
##WHILE
n = 5
while n > 0:
n -=1
print(n)
#WHILE BREAK
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('Loop ended.')
#WHILE CONTINUE
n = 5
while n > 0:
n -= 1
if n == 2:
continue
print(n)
print('Loop ended.')
#WHILE ELSE
n = 5
while n > 0:
n -= 1
print(n)
if n == 2:
break
else:
print('Loop done.')
#ONELINE WHILE
n =5
while n > 0: n -= 1; print(n)
#INTERACTIVE LOOPS
#WHILE
lines = list()
testAnswer = input('Press y if you want to enter more lines: ')
while testAnswer == 'y':
line = input('Next line: ')
lines.append(line)
testAnswer = input('Press y if you want to enter more lines: ')
print('Your lines were:')
for line in lines:
print(line)
#FOR
lines = list()
n = int(input('How many lines do you want to enter? '))
for i in range(n):
line = input('Next line: ')
lines.append(line)
print('Your lines were:') # check now
for line in lines:
print(line)