简体   繁体   English

AttributeError:'list'对象没有属性'split'-Python Progject

[英]AttributeError: 'list' object has no attribute 'split' - Python Progject

Need help with my code, trying to create a music game. 需要有关我的代码的帮助,尝试创建音乐游戏。 I am getting a 'AttributeError: 'list' object has no attribute 'split'. 我收到一个'AttributeError:'list'对象没有属性'split'。 Help would be appreciated. 帮助将不胜感激。

Thanks 谢谢

 import random read = open("Songs.txt", "r") songs = read.readlines() songlist = [] #readlines - https://stackoverflow.com/questions/38105507/when-should-i-ever-use-file-read-or-file-readlines for i in range(len(songs)): songlist.append(songs[i].strip('\\n')) songname = random.choice(songlist) #https://pynative.com/python-random-choice/ - random choice songs = songs.split() letters = [word[0] for word in songs] firstguess = input("Please enter your first guess for the name of the song.") if firstguess is True: print("Well done, you have been awarded 3 points.") elif firstguess is False: print("Unlucky, that answer is incorrect. Please try again to win 1 point.") secondguess = input("Please enter your second guess for the name of the song. Last try, make sure to think before typing!") if secondguess is True: print("Well done, you managed to clutch 1 point.") elif secondguess is False: print("Unlucky, maybe you will know the next song.") 

Please refer also to this question: Attribute Error: 'list' object has no attribute split 请同时参考以下问题: 属性错误:“列表”对象没有属性拆分

From this you can see that split is an attribute of strings not lists! 从中您可以看到split是字符串而不是列表的属性! The solution from the above question was to split each string/line of the list, so use something like: 上述问题的解决方案是拆分列表的每个字符串/行,因此使用类似以下内容的方法:

songs = [sentence.split() for sentence in songs]

To be clear here, sentence is an arbitrary iterator and can be named more appropriately (eg like verse) for your purpose. 在这里要清楚的是, sentence是一个任意的迭代器,可以根据您的目的更恰当地命名(例如,像verse)。 Even though it does not matter to the code execution, good naming is always helpful. 即使对代码执行无关紧要,但良好的命名总是有帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 2:AttributeError:'list'对象没有属性'split' - Python 2 : AttributeError: 'list' object has no attribute 'split' AttributeError: 'list' 对象在 Python 中没有属性 'split' - AttributeError: 'list' object has no attribute 'split' in Python AttributeError: 'list' object 没有属性 'split' - Python - AttributeError: 'list' object has no attribute 'split' - Python “AttributeError:'list'对象的问题在Python中没有属性'split'” - Problems with “AttributeError: 'list' object has no attribute 'split'” in Python Python 3.7 AttributeError:“列表”对象没有属性“分割” - Python 3.7 AttributeError: 'list' object has no attribute 'split' AttributeError: 'list' 对象没有属性 'split' - AttributeError: 'list' object has no attribute 'split' AttributeError: 'list' object 没有属性 'split' -PyTorch - AttributeError: 'list' object has no attribute 'split' -PyTorch Python:AttributeError:'NoneType'对象没有属性'split' - Python:AttributeError: 'NoneType' object has no attribute 'split' AttributeError: 'Timestamp' object 在 python 中没有属性 'split' - AttributeError: 'Timestamp' object has no attribute 'split' in python Spotipy: AttributeError: 'list' object 没有属性 'split' - Spotipy: AttributeError: 'list' object has no attribute 'split'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM