-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_while_loop.py
More file actions
158 lines (113 loc) · 2.04 KB
/
Copy path6_while_loop.py
File metadata and controls
158 lines (113 loc) · 2.04 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
# ==========================
# PYTHON WHILE LOOP CHEAT SHEET
# ==========================
# 1. Simple while loop
i = 1
while i <= 5:
print(i)
i += 1
# 2. Print even numbers
i = 2
while i <= 10:
print(i)
i += 2
# 3. Print odd numbers
i = 1
while i <= 10:
print(i)
i += 2
# 4. Reverse counting
i = 10
while i >= 1:
print(i)
i -= 1
# 5. Infinite loop
while True:
print("Hello")
# 6. break
i = 1
while i <= 10:
if i == 6:
break
print(i)
i += 1
# 7. continue
i = 0
while i < 10:
i += 1
if i == 5:
continue
print(i)
# 8. pass
i = 1
while i <= 5:
pass
i += 1
# 9. Nested while loop
i = 1
while i <= 3:
j = 1
while j <= 3:
print(i, j)
j += 1
i += 1
# 10. Multiplication table
num = 5
i = 1
while i <= 10:
print(num * i)
i += 1
# 11. Sum of numbers
i = 1
total = 0
while i <= 10:
total += i
i += 1
print(total)
# 12. Factorial
num = 5
fact = 1
i = 1
while i <= num:
fact *= i
i += 1
print(fact)
# 13. Loop through a string
name = "Python"
i = 0
while i < len(name):
print(name[i])
i += 1
# 14. Loop through a list
numbers = [10, 20, 30]
i = 0
while i < len(numbers):
print(numbers[i])
i += 1
# 15. Password check
password = "python123"
attempts = 3
while attempts > 0:
user_password = input("Enter Password: ")
if user_password == password:
print("Login Successful")
break
else:
attempts -= 1
print("Wrong Password")
print("Attempts Left:", attempts)
if attempts == 0:
print("Account Locked")
# ==========================
# QUICK NOTES
# ==========================
# while
# Runs until the condition becomes False.
# break
# Exit the loop immediately.
# continue
# Skip the current iteration.
# pass
# Placeholder; does nothing.
# Nested while
# A while loop inside another while loop.