我有一个名为newSong的文本字符串,其中包含两个instancevariabel(title,artist),如下所示:
newSong = Song ("Rum and Raybans", "Sean Kingston and Cher Lloyd")
我有一个名为checkIfArtistExists(self, artist)
的方法。我的任务是使用split、for循环和if语句。我必须拆分艺术家,所以如果艺术家有肖恩,金斯顿,雪儿,劳埃德组成的名字,它将返回真,否则假
我得到资产错误:
>>> assert(not newSong.checkIfArtistExsists("Sadley")) # False
AssertionError
我是新来的编程和我的逻辑不是很好。。。有人能给我建议或小费吗
class Songs(object):
def __init__(self, tittel, artist):
#Instanse variabler
self._tittel = tittel
self._artist = artist
def CheckIfArtistExists(self,artist):
names = artist.split()
for n in names:
if n in artist:
return true
else:
return false
newSong = Song ("Rum and Raybans", "Sean Kingston and Cher Lloyd")
assert(newSong.CheckIfArtistExists("Sean Kingston and Cher Lloyd"))
assert(not newSong.CheckIfArtistExists(""Sadley"")) #False
您测试的是
artist
参数本身,而不是实例变量_artist
。这就是为什么newSong.checkIfArtistExists("Sadley")
是真的,而它应该是假的一旦找到匹配项,循环就会退出。它应该等到它已经测试了所有被分割的部分都是子字符串
相关问题 更多 >
编程相关推荐