-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_while_loop.py
More file actions
75 lines (68 loc) · 2.75 KB
/
Copy path16_while_loop.py
File metadata and controls
75 lines (68 loc) · 2.75 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
#======================================================================================
# WHILE LOOPS
#--------------------------------------------------------------------------------------
# A while loop keeps running as long as a condition is True.
# When the condition becomes False, the loop stops.
#======================================================================================
#--------------------------------------------------------------------------------------
# while loop categories
#--------------------------------------------------------------------------------------
# 1)while condition--> used when repetition depends on a condition
# 2)while True--> runs forever unless stopped using break or ctrl + c(Risky)
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# while condition
#--------------------------------------------------------------------------------------
i = 0 #------------->Initialization
while i <= 5: #----->Condition
print(i)
i += 1 #-------->Update
#--------------------------------------------------------------------------------------
# while True
#--------------------------------------------------------------------------------------
while True:
x = input("Type: ")
if x == "stop":
break
#--------------------------------------------------------------------------------------
# Task
# write a program that keeps asking "Do you agree?" until user types "yes"
#--------------------------------------------------------------------------------------
# while condition
answer = ""
while answer != "yes":
answer = input("Do you agree?(yes/no): ")
print("Thank You")
# while True
while True:
answer = input("Do you agree?(yes/no):")
if answer == "yes":
break
#--------------------------------------------------------------------------------------
# challenge
# write a program that keeps asking "Do you agree?" until user types "yes"
# allow up to 3 attempts
# if the user types "yes", print "Glad we are on the same page"
# otherwise, print "3 strikes, you are out!"
#--------------------------------------------------------------------------------------
# by using while condition
attempts = 0
while attempts < 3:
answer = input("Do you agree?(yes/no): ")
if answer == "yes":
print("Glad we're on the same page")
break
attempts += 1
else:
print("3 strikes. you are out!")
# by using while True
count = 0
while True:
answer = input("Do yo agree?(yes/no): ")
count += 1
if answer == "yes":
print("Glad we're on the same page")
break
if count == 3:
print("3 strikes. You are out!")
break