当我运行它时,它给我这个错误代码
Good day sir: Dog play music
Traceback (most recent call last):
File "F:\Python34\My_Friend\test.py", line 23, in <module>
os.startfile(song)
TypeError: Can't convert 'NoneType' object to str implicitly
if(next == "Dog play music"):
music = ['Asshole.mp3', 'Berzerk.mp3', 'Brainless.mp3',
'Rap_God.mp3', 'Rabbit_Run.mp3','Lose_Yourself.mp3', 'Survival.mp3']
song = random.shuffle(music)
stop = False
while(stop == False):
os.startfile(song)
stop = True
user = input(song + " has been played: ")
if(user == "Dog im done"):
os.startfile('test.py')
os.close('test.py')
if(user == "Dog play next"):
stop = False
答案 0 :(得分:1)
错误在于这一行:
song = random.shuffle(music)
random.shuffle
不会返回任何内容:它会重新排序列表。以下方法可行:
random.shuffle(music)
song = music[0]
或者更简单:
song = random.choice(music)
可以在命令行上测试:
>>> import random
>>> x = range(9)
>>> random.shuffle(x)
>>> x
[1, 4, 2, 3, 0, 5, 6, 7, 8]
>>>
请注意,random.shuffle(x)
行没有返回任何内容。但是,列表x
现在是随机排列的。
或者,使用random.choice
:
>>> x = range(9)
>>> random.choice(x)
2
>>> random.choice(x)
6
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8]
使用random.choice
,列表x
保持原始顺序。每次运行random.choice
时,都会返回列表的随机成员。