問題:請勿在您的函數中使用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