forked from peterrowland/D04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW04_ch08_ex04.py
76 lines (58 loc) · 2.44 KB
/
HW04_ch08_ex04.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
#!/usr/bin/env python
# HW04_ch08_ex04
# 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 (is the docstring) what the function actually does.
# You can assume that the parameter is a string.
# Do not merely paste the output as a counterexample into the documentation
# string, explain what is wrong.
###############################################################################
# Body
def any_lowercase1(s):
# Explain what is wrong, if anything, here.
# This function evaluates only the first letter of the string and exits the execution.
for c in s:
if c.islower():
return True
else:
return False
def any_lowercase2(s):
# Explain what is wrong, if anything, here.
# What I supposse is wrong with this function is that it is evaluating the letter 'c', not the string contained in the variable c.
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
def any_lowercase3(s):
# Explain what is wrong, if anything, here.
# What is wrong is that it only evaluates the last letter.
for c in s:
flag = c.islower()
return flag
def any_lowercase4(s):
#Explain what is wrong, if anything, here.
# What is wrong is that in the first execution the variable 'flag' can get the value 'True' and after that it will always returns 'True' no matter what
# happens with the sentence 'c.islower()'. It will only work partially if the fisrt letter is capital.
flag = False
for c in s:
flag = flag or c.islower()
#print(c + ' ' + str(flag))
return flag
def any_lowercase5(s):
# Explain what is wrong, if anything, here.
# This function works fine for me, the function evaluates each letter and quits the the execution as soon as it finds the first capital letter.
for c in s:
if not c.islower():
return False
return True
###############################################################################
def main():
# Remove print("Hello World!") and for each function above that is wrong,
# call that function with a string for which the function returns
# incorrectly.
# ex.: any_lowercase_("thisstringmessesupthefunction")
#print("Hello World!")
print(any_lowercase5("hisstringmefsFesupthefunction"))
if __name__ == '__main__':
main()