4) a. While Loop Examples




You’re the city’s detective. In a recent incident, you investigated a crime scene and discovered that the lead criminal had a phone number starting with number 13. You’ve set out a trap door that will screen all city dwellers but trap only the person whose number begins with 13 by ending program execution thus shutting the door permanently until you will open it manually. Write a python program that will take the first two digits of a person’s phone number and stop executing when a user enters number 13.

1
2
3
4
5
Output:
Enter your phone number :  1442
You may pass through the door
Enter your phone number :  1354
Please step aside!




Your friend Robert never operates his chemist beyond 2200hrs. However, because of a recent disease outbreak, he’s decided to shut his door after the 5th customer visiting after 2200hrs. Use a while loop in a python code snippet that allows the first 5 clients to his chemist after 2200hrs and then prints a “closed shop” message.

1
2
3
4
5
6
7
Output:
Welcome to my store! Please press a key to enter.
Welcome to my store! Please press a key to enter.
Welcome to my store! Please press a key to enter.
Welcome to my store! Please press a key to enter.
Welcome to my store! Please press a key to enter.
I'm sorry, the store is closed for today. Please come back tomorrow!




Count Vlad Marek is a vampire who hates his corporate job! Having to work in a cubicle while the devastating sun rays slice-in through the glass windows is horrible already. But what’s pure torture is that there’s always scrumptious food walking all around him. He keeps checking the time to see how many seconds are remaining for the day before he can storm out the door.

Write a Python program that displays a countdown starting from 100. At every 10th count it should display the message “Be strong Vlad! You will get through this.” (Hint: To check for 10th count just divide the counter by 10 and check whether remainder is 0, i.e.,   counter % 10 == 0 ).

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
Output:
99
98
97
96
95
94
93
92
91
90
Be strong Vlad! You will get through this.
89
88
87
86
85
84
83
82
81
80
Be strong Vlad! You will get through this.
79
78
77
76
75
74
73
72
71
70
Be strong Vlad! You will get through this.
69
68
67
66
65
64
63
62
61
60
Be strong Vlad! You will get through this.
59
58
57
56
55
54
53
52
51
50
Be strong Vlad! You will get through this.
49
48
47
46
45
44
43
42
41
40
Be strong Vlad! You will get through this.
39
38
37
36
35
34
33
32
31
30
Be strong Vlad! You will get through this.
29
28
27
26
25
24
23
22
21
20
Be strong Vlad! You will get through this.
19
18
17
16
15
14
13
12
11
10
Be strong Vlad! You will get through this.
9
8
7
6
5
4
3
2
1
0
Be strong Vlad! You will get through this.