首页 > 解决方案 > 虚拟机器人 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]

标签: pythonrandombots

解决方案


问题是因为random.choices()返回“一个k大小的元素列表......”,所以你需要使用:

choose = random.choices(game)
os.startfile[choose[0])

由于显而易见的原因,在这里使用可能会更好random.choice()......</p>

choice = random.choice(game)
os.startfile(choice)

推荐阅读