0

I made a script that technically works, but doesn't give me the output that I want, which is the choice from a user-supplied list. At first, it simply returned the index of the option, not the string. I looked at this thread, and tried using random.choice() instead of random.randint(), which only returned the list instead.

import random
import time as t
def choose():
    choices = []
    question = input('What are your options? (Please enter comma separated):\n').split(',')
    choices.append(question)

    print(random.choice(choices))
    t.sleep(5000)
    
choose()

1 Answers1

0

The code you provided will allow the user to enter their options as a comma-separated string and store those options in a list. Then, the random.choice function will pick a random option from the list and print it to the screen.

Well, however, there are a few issues with this code. The choices.append line is adding the entire question list as a single item to the choices list, which means that the random.choice function will only pick one option from the question list, not from the individual options that were entered. To fix this, you should use the extend method instead of the append method:

import random
import time as t

def choose():
    choices = []
    question = input('What are your options? (Please enter comma separated):\n').split(',')
    choices.extend(question)

    print(random.choice(choices))
    t.sleep(5)
    
choose()

good luck

Sid Nejad
  • 16
  • 1
  • 1
    Just skip the `choices` variable altogether and just use `random.choice(question)`. It is good to know about the difference of `append` and `extend`, though. – B Remmelzwaal Feb 10 '23 at 22:59
  • Thank you so much for the input, both of you! I wound up using ```random.choice(question)``` and assigning it to a variable, then printing it. I'm learning python, among some other languages. – Arcade Luke Mar 15 '23 at 19:03