python - 虚拟机器人 random.choices 和 os.startfile()
问题描述
我正在使用许多模块在 Python 上制作这个虚拟机器人,其中一些模块是(操作系统和随机的),所以在这个虚拟机器人中,它问我想做什么,我回应它,并根据我的询问或告诉它给我结果去做。
所以,我试图在这段代码中添加另一个功能,我让它玩一个游戏,它问我是哪个游戏,我做出回应,它打开了我告诉它的游戏。
elif "jarvis play a game" in query or "jarvis play one of my games" in query or "jarvis play my games" in query:
speak("ok", "I have 6 games", "they are", "breakout arcade game", "car arcade game", "obstrucal course game", "football champs", "path finder", "and", "tic tac toe", "which of these games would you like to play?")
wgp = takeCommand().lower()
if wgp == "breakout arcade game":
speak("ok, opening breakout arcade game, have fun")
os.startfile("breakout arcade gaming.py")
elif wgp == "car arcade game":
speak("ok, opening car arcade game, have fun")
os.startfile("car arcade gaming.py")
elif wgp == "obstrucal course game":
speak("ok, opening obstrucal course game, have fun")
os.startfile("complete obby game! - ursina.py")
elif wgp == "football champs":
speak("ok, opening football champs, have fun")
os.startfile("football champs.py")
elif wgp == "path finder":
speak("ok, opening path finder, have fun")
os.startfile("path finder.py")
elif wgp == "tic tac toe":
speak("ok, opening tic tac toe, have fun")
os.startfile("tic tac toe2options.py")
elif wgp == "choose a random game":
speak("ok")
game = ["tic tac toe2options.py","football champs.py","complete obby game! - ursina.py"]
choose = random.choices(game)
os.startfile(choose)
这只是代码的一部分,因此elif
语句不是错误,查询是听到并保存我们所说的内容。因此,在这段代码中,一切正常,但最后一条elif
语句是我尝试让它在它们保存为的给定文件名之间选择一个随机游戏,它应该选择其中一个,我希望os.startfile()
启动一个在给定列表中的那些。但是,当我运行此代码时,它给了我一个错误消息:
line 24, in <module>
os.startfile(choose)
TypeError: startfile: filepath should be string, bytes or os.PathLike, not list
[Finished in 0.6s with exit code 1]
[shell_cmd: python -u "C:\Users\Vatsa\OneDrive\Desktop\New folder\all main py files\pyautogui basics 2.py"]
[dir: C:\Users\Vatsa\OneDrive\Desktop\New folder\all main py files]
[path: C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Android;C:\Windows\System32;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\;C:\Users\Vatsa\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\bin;C:\Users\Vatsa\AppData\Local\GitHubDesktop\bin;C:\Users\Vatsa\AppData\Local\atom\bin;C:\Users\Vatsa\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Vatsa\.dotnet\tools]
解决方案
问题是因为random.choices()
返回“一个k大小的元素列表......”,所以你需要使用:
choose = random.choices(game)
os.startfile[choose[0])
由于显而易见的原因,在这里使用可能会更好random.choice()
......</p>
choice = random.choice(game)
os.startfile(choice)
推荐阅读
- javascript - localStorage 返回 null,即使它可以通过浏览器登录到控制台
- python - 无法理解 __lt__ 方法
- html - 即使在使用 Multer 和 HTML 的 Nodejs 项目中表单提交失败时,图像也会保存
- javascript - 如何在 JavaScript 中执行 GPS 坐标三角测量?
- javascript - 更改下拉 Javascript 后结果不更新
- reactjs - 我应该使用什么类型来匹配返回 div 的 FunctionComponent?
- c - 正确使用“volatile”是否仍然(总是)导致程序与该数据具有未定义的交互?
- java - 将 MongoDB Java 驱动程序添加为 Maven 依赖项的问题
- javascript - 在javascript中从多个文件中读取数据
- php - 如何使用 REST 接口退出 WordPress?