-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhile_exercise.py
More file actions
36 lines (29 loc) · 799 Bytes
/
while_exercise.py
File metadata and controls
36 lines (29 loc) · 799 Bytes
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
#!/usr/bin/env python
"""
sample of using while with break and continue and else
"""
def count_them(letter):
count = 0
while True:
in_letter = raw_input("give me a letter (x to stop)")
print "you gave me:", in_letter
if in_letter == letter:
count += 1
if in_letter == 'x':
break
print "there were:", count, "instances of the letter:", letter
return count
def count_them2(string, letter):
"""
counts the number of instances of the letter in the string
ends when a period is encountered
"""
count = 0
for l in string:
if l == '.':
break
if l == letter:
count = count+1
else:
print "hey, there was no period!"
return count