所以我知道这是非常肮脏且可能效率低下的代码,但我只是想看看我是否可以让它工作。我只是不明白为什么它不起作用......我正在访问的文件中的文本位于http://www.pythonchallenge.com/pc/def/equality.html的来源中。有什么帮助吗?
#! /usr/bin/python
# This program searches a string for a lowercase letter that has 3, and only 3,
# uppercase letters to the left of it, and 3, and only 3, uppercase letters to
# the right of it. It then returns that lowercase letter.
from string import lowercase
from string import uppercase
# Open the text file containing the string and set it to variable str
fileHandle = open ( 'bodyguards.txt', 'r' )
str = fileHandle.read()
fileHandle.close()
#str = 'BIGaBIGaBIGaBIG'
# Search for a lowercase letter.
def isitlower(str):
for a in str :
if a in lowercase:
letterposition = str.index(a)
r =threebgright(letterposition)
l =threebgleft(letterposition)
if r !=None and l !=None:
print l,":",a,":", r
def threebgright(letterposition):
if str[letterposition + 1] in uppercase and str[letterposition +2] in uppercase and str[letterposition + 3] in uppercase and str[letterposition + 4] not in uppercase:
return str[letterposition+1], str[letterposition+2], str[letterposition+3]
else:
return None
def threebgleft(letterposition):
if str[letterposition - 1] in uppercase and str[letterposition -2] in uppercase and str[letterposition - 3] in uppercase and str[letterposition - 4] not in uppercase:
return str[letterposition-1], str[letterposition-2], str[letterposition-3]
else:
return None
isitlower(str)