-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex_08_11.py
62 lines (51 loc) · 1.71 KB
/
ex_08_11.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
#!/usr/bin/python
#AUTHOR: alexxa
#DATE: 25.12.2013
#SOURCE: Think Python: How to Think Like a Computer Scientist by Allen B. Downey
# http://www.greenteapress.com/thinkpython/html/index.html
#PURPOSE: Chapter 8. Strings
# Exercise 8.11
# The following functions are all intended to check whether
# a string contains any lowercase letters, but at least some
# of them are wrong. For each function, describe what the
# function actually does (assuming that the parameter is a string).
def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
# any_lowercase1(s) is wrong since it tests for lowercase/uppercase
# only the first letter of the string.and returns True/False respectively
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
# any_lowercase2(s) is wrong since it returns True. It checks whether
# 'c' is lowercase and this function doesn't check any letter from the given string
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
# any_lowercase3(s) is wrong since it returns True/False depending only on the last
# letter of the string.
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
# any_lowercase4 is fine!
def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
# any_lowercase5(s) is wrong since it checks whether ALL letters in a string are lowercase
print(any_lowercase1('Banana'))
print(any_lowercase2('BANANA'))
print(any_lowercase3('bananA'))
print(any_lowercase4('bANANA'))
print(any_lowercase5('bananA'))
#END