-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile.py
More file actions
executable file
·175 lines (134 loc) · 2.99 KB
/
while.py
File metadata and controls
executable file
·175 lines (134 loc) · 2.99 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 9 23:11:57 2021
@author: maherme
"""
#%%
i = 0
while i < 5:
print(i)
i += 1
#%%
# In python does not exist "do while" loop, but you can create as following.
# Notice that "something" will never be printed.
i = 5
while True:
print(i)
if i >= 5:
break
print('something')
#%%
min_length = 2
name = input("Please enter your name:")
while not(len(name) >= min_length and name.isprintable() and name.isalpha()):
name = input("Please enter your name:")
print("Hello, {0}".format(name))
#%%
# A better way to do the code above (avoiding line repetition):
min_length = 2
while True:
name = input("Please enter your name:")
if len(name) >= min_length and name.isprintable() and name.isalpha():
break
print("Hello, {0}".format(name))
#%%
# continue statement:
a = 0
while a < 10:
a += 1
if a % 2 == 0:
continue
print(a)
#%%
# else statement: look the following code
l = [1, 2, 3]
val = 10
found = False
idx = 0
while idx < len(l):
if l[idx] == val:
found = True
break
idx += 1
if not found:
l.append(val)
print(l)
#%%
# else statement: a better way to do the code above is using else statement:
l = [1, 2, 3]
val = 10
idx = 0
while idx < len(l):
if l[idx] == val:
break
idx += 1
else:
l.append(val)
print(l)
#%%
# This is a try statement:
a = 10
b = 0
try:
a/b
except ZeroDivisionError:
print('division by 0')
finally:
print('this always executes')
#%%
# Let see the try statment into a loop.
# Notice that the finally statement is executed always.
a = 0
b = 2
while a < 4:
print('--------------------')
a += 1
b-= 1
try:
a/b
except ZeroDivisionError:
print("{0}, {1} - division by 0".format(a, b))
continue
finally:
print("{0}, {1} - always executes".format(a, b))
print("{0}, {1} - main loop".format(a, b))
#%%
# Let see the try statment into a loop.
# Notice that the finally statement is executed always with break also
a = 0
b = 2
while a < 4:
print('--------------------')
a += 1
b-= 1
try:
a/b
except ZeroDivisionError:
print("{0}, {1} - division by 0".format(a, b))
break
finally:
print("{0}, {1} - always executes".format(a, b))
print("{0}, {1} - main loop".format(a, b))
#%%
# Let see the try statment into a loop.
# Notice that the else statement is only executed when the exception does not
# happen
a = 0
# b = 2
b = 10
while a < 4:
print('--------------------')
a += 1
b-= 1
try:
a/b
except ZeroDivisionError:
print("{0}, {1} - division by 0".format(a, b))
break
finally:
print("{0}, {1} - always executes".format(a, b))
print("{0}, {1} - main loop".format(a, b))
else:
print('Code executed without a zero division error')
#%%