简体   繁体   English

我在弦上遇到麻烦

[英]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

The error is with this line: 错误是与此行:

song = random.shuffle(music)

random.shuffle does not return anything: it reorders the list in place. random.shuffle不返回任何内容:它将对列表进行重新排序。 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. 请注意, random.shuffle(x)random.shuffle(x)返回任何内容。 The list, x , though, is now in random order. 但是,列表x现在处于随机顺序。

Alternatively, using random.choice : 或者,使用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. 使用random.choice ,列表x保持原始顺序。 Each time random.choice is run, a random member of the list is returned. 每次运行random.choice ,都会返回列表的随机成员。

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

相关问题 我在使用Python函数时遇到麻烦,有人可以帮我吗? - I am having troubles with Python functions, can someone help me? 我在 python 中的文件和目录上测试模块时遇到问题 - I am having troubles testing the module on file and on directory in python 我在学习如何使用 STR、INT 和 ELIF 时遇到问题 - I am having troubles with learning how to use STR and INT and ELIF 我在获取列表而不是绑定方法时遇到问题 - I am having troubles getting the list instead of an bound method 我在处理来自Django的404错误消息时遇到了麻烦。 它说“找不到与查询匹配的位置” - I am having troubles with a 404-error message from Django. It says “No location found matching the query” 我在使用 Uber Python API 时遇到了麻烦 - I'm having troubles with Uber Python API 在将迁移应用到翻译后,django-parler 2.0.1 出现问题,它不会在管理站点中显示产品字段 - Am having troubles with django-parler 2.0.1 after i had applied migration to translations, it won't show Products fields in admin site 我很难理解为什么字符串索引超出范围 - I am having difficulty understanding why the string index is out of range 我在将列表作为字符串执行自动化功能时遇到问题 - I Am Having Trouble Executing a list as a string for an automated function 我有麻烦让pybrain通过anaconda工作 - I'm having troubles getting pybrain working through anaconda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM