Lee Strong
Lee Strong

Reputation: 1

I am having troubles with a string

It is giving me this error code when i run it

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

Upvotes: 0

Views: 48

Answers (1)

John1024
John1024

Reputation: 113964

The error is with this line:

song = random.shuffle(music)

random.shuffle does not return anything: it reorders the list in place. The following would work:

random.shuffle(music)
song = music[0]

Or, more simply:

song = random.choice(music)

Example

This can be tested on the command line:

>>> import random
>>> x = range(9)
>>> random.shuffle(x)
>>> x
[1, 4, 2, 3, 0, 5, 6, 7, 8]
>>> 

Note that the line random.shuffle(x) returned nothing. The list, x, though, is now in random order.

Alternatively, using random.choice:

>>> x = range(9)
>>> random.choice(x)
2
>>> random.choice(x)
6
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8]

With random.choice, the list x remains in its original order. Each time random.choice is run, a random member of the list is returned.

Upvotes: 1

Related Questions