第一题是:
def same_first_name(name1, name2):
""" (list of str, list of str) -> bool
Return whether the first element of name1 and name2 are the same.
>>> same_first_name(['John', 'Smith'], ['John', 'Harkness'])
True
>>> same_first_name(['John', 'Smith'], ['Matt', 'Smith'])
False
第二题是:
def search_closet(items, colour):
""" (list of str, str) -> list of str
items is a list containing descriptions of the contents of a closet where
every description has the form 'colour item', where each colour is one word
and each item is one or more word. For example:
['grey summer jacket', 'orange spring jacket', 'red shoes', 'green hat']
colour is a colour that is being searched for in items.
Return a list containing only the items that match the colour.
>>> search_closet(['red summer jacket', 'orange spring jacket', 'red shoes', 'green hat'], 'red')
['red summer jacket', 'red shoes']
>>> search_closet(['red shirt', 'green pants'], 'blue')
[]
>>> search_closet([], 'mauve')
[]
"""
然后第二题我写了这个,但是不对,说search_closet(['Red Foo', 'Far bar', 'Boo rally', 'Cat Black'], 'Black')时候无法正确运行,求帮我改一下!
out_list = []
for item in items:
if colour in item:
out_list.append(item)
return out_list
1,第一题:
def same_first_name(name1, name2):运行结果:
True
False
2,第二题:
def search_closet(items, colour):运行结果:
['red summer jacket', 'red shoes']
3,说明:
第二题那个
if colour in item:在我这运行成功的,如果你那不行就把把item按空格' '进行分割为一个list
其次,把第一题的参数判断的几个if else,你自己移到第二题上感受下.
追问第一题不行。。。
第一题不行。。。
追答def same_first_name(name1, name2):那就简单点吧,对了你不是在Python IDLE里面直接全粘贴的吧?要先贴那个函数,后运行下面两个print
还有如果你是python3,需要把print xxx 改为print(xxx)
追问还是不对呃,name1里有两个名字、,name2也是,要4个名字的首字母都一样
追答不明白,你输入的什么name?发这里,我看看.