-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhile.py
90 lines (80 loc) · 2.47 KB
/
while.py
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
raw_input('a while loop example')
driving_age = 16
smoking_age = 18
voting_age = 18
drinking_age = 21
curmudgeon_age = 35
social_security_age = 65
max_age = 100
age = 1
you_can = 'at age {0} you can {1}'
while age <= max_age:
stuff_you_can_do = []
if age >= driving_age:
stuff_you_can_do.append('drive')
if age >= smoking_age:
stuff_you_can_do.append('smoke')
if age >= voting_age:
stuff_you_can_do.append('vote')
if age >= drinking_age:
stuff_you_can_do.append('drink')
if age >= curmudgeon_age:
stuff_you_can_do.append('tell kids they bother you')
if len(stuff_you_can_do) == 0:
print you_can.format(age,'do nothing of interest')
elif len(stuff_you_can_do) == 1:
print you_can.format(age,stuff_you_can_do[0])
else:
last_one = stuff_you_can_do.pop()
print you_can.format(age,', '.join(stuff_you_can_do)) + ', and ' + last_one
age+=1
raw_input('first while loop done')
done = False
while not done:
inp = raw_input('enter an age (or "done" to exit): ')
if inp == 'done':
done = True
elif not inp.isdigit():
print 'please enter an integer'
else:
age = int(inp)
stuff_you_can_do = []
if age >= driving_age:
stuff_you_can_do.append('drive')
if age >= smoking_age:
stuff_you_can_do.append('smoke')
if age >= voting_age:
stuff_you_can_do.append('vote')
if age >= drinking_age:
stuff_you_can_do.append('drink')
if age >= curmudgeon_age:
stuff_you_can_do.append('tell kids they bother you')
if len(stuff_you_can_do) == 0:
print you_can.format(age,'do nothing of interest')
elif len(stuff_you_can_do) == 1:
print you_can.format(age,stuff_you_can_do[0])
else:
last_one = stuff_you_can_do.pop()
print you_can.format(age,', '.join(stuff_you_can_do)) + ', and ' + last_one
raw_input('second while loop done')
shifts_coverable = [2,4,1,3,2,5]
shifts_needed = 11
i = 0
shifts_covered = 0
while shifts_covered < shifts_needed:
shifts_covered += shifts_coverable[i]
i += 1
print i
raw_input('while list done')
try:
shifts_coverable = [2,4,1,3,2,5]
shifts_needed = 20
i = 0
shifts_covered = 0
while shifts_covered < shifts_needed:
shifts_covered += shifts_coverable[i]
i += 1
print i
except Exception as error:
print error
raw_input('bad list while done')