问题:请勿在您的函数中使用set:使用列表返回第一个和最后一个名称中的常见字母列表(交叉点)提示用户名字和姓氏,并以名字和姓氏作为参数来调用函数并打印返回的列表。Python函数返回第一个和最后一个名字中的常见字母列表
我不明白为什么我的程序只是打印“不匹配”,即使有字母匹配。任何帮助!谢谢一堆!
到目前为止的代码:
import string
def getCommonLetters(text1, text2):
""" Take two strings and return a list of letters common to
both strings."""
text1List = text1.split()
text2List = text2.split()
for i in range(0, len(text1List)):
text1List[i] = getCleanText(text1List[i])
for i in range(0, len(text2List)):
text2List[i] = getCleanText(text2List[i])
outList = []
for letter in text1List:
if letter in text2List and letter not in outList:
outList.append(letter)
return outList
def getCleanText(text):
"""Return letter in lower case stripped of whitespace and
punctuation characters"""
text = text.lower()
badCharacters = string.whitespace + string.punctuation
for character in badCharacters:
text = text.replace(character, "")
return text
userText1 = raw_input("Enter your first name: ")
userText2 = raw_input("Enter your last name: ")
result = getCommonLetters(userText1, userText2)
numMatches = len(result)
if numMatches == 0:
print "No matches."
else:
print "Number of matches:", numMatches
for letter in result:
print letter
的问题似乎是你Python语法... – JBernardo 2012-03-16 20:36:52