-1

I was making this virtual bot on Python using many modules out of which some were (os and random) so in this virtual bot it asks me what I want to do I respond to it and gives me outcomes based on what I asked or told it to do.

So, I was trying to add another feature in this code, where, I ask it to play a game it asks me which game and I respond and it opens the game I tell it to.

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)

This is only a part of the code so the elif statement is not an error, query is the thing that hears and saves what we say. So in this code everything works but the last elif statement where I try making it choose a random game between the given file names that they are saved as, it should choose one of those and I want the os.startfile() to start a file that was randomly chosen between the given ones in the list. However when I run this code it gives me an error saying:

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]
Peter O.
  • 32,158
  • 14
  • 82
  • 96
vatsal
  • 3
  • 6

1 Answers1

0

The problem is because random.choices() returns "a k sized list of elements…", so you would need to use:

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

For obvious reasons, it would probably be better to use random.choice() here…

choice = random.choice(game)
os.startfile(choice)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • ohh great! it works now thank you so much also i had a question if we could like decrease the chance of the same thing to repeat is there any way we cud do that? – vatsal Mar 29 '21 at 12:05
  • [Random without repetition?](https://stackoverflow.com/questions/30735892/random-without-repetition) – martineau Mar 29 '21 at 13:16