我遇到麻烦了

时间:2014-09-06 02:38:12

标签: python

当我运行它时,它给我这个错误代码

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

1 个答案:

答案 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时,都会返回列表的随机成员。