我想定義一個名爲all_extreme()的函數,該函數使用一個字符串元組,並且如果所有字符串都包含感嘆號,則返回True
。如果任何字符串缺少感嘆號,則應返回False
。這個函數需要使用我已經定義的函數extreme()。所以all_extreme(("!","Bogus!","!YES!"))
和all_extreme(("Nifty!","!!"))
都應返回True
,但all_extreme((「Cool!」,「好的......」))和all_extreme(("square","..."))
都應該返回False
。以下是我迄今爲止:用於循環的AttributeError
def extreme(x):
"""returns True if the string contains at least one exclamation point
str -> str"""
if x.find('!') != -1:
return True
else:
return False
def all_extreme(x):
"""returns true if all of the strings contain exclamation points
str, str... -> str"""
for word in x:
if extreme(x) == True:
return True
else:
return False
,我遇到的問題是,我得到一個AttributeError的:「元組」對象有沒有屬性「發現」。我願意接受任何和所有建議。
極端可以簡單地'返回x.find( '!')= -1' –
或'回報在x'中。此外,你的循環只會測試列表的第一項。 – kindall