0

I'm trying to make a random list thing in python. Every time you run the code, random words from a list would appear in order. What I tried to do was this:

import random
numSelect = 0
list = ['thing1', 'thing2', 'thing3', 'thing4', 'thing5']
for i in range(random.randint(1, 3)):
    rThing = random.choice(list)
    numSelect = numSelect + 1
    print(numSelect, '-' , rThing)

The objective is that the user is asked to choose something from the list that would display. Here is an example of the output that I want:

1 - thing4

2 - thing2

Which one do you choose?: 

(User would type '2')

*output of thing2*
MackM
  • 2,906
  • 5
  • 31
  • 45
Dbbo
  • 13
  • 1
  • 1
    Possible duplicate of [Select 50 items from list at random to write to file](https://stackoverflow.com/questions/15511349/select-50-items-from-list-at-random-to-write-to-file) – Brown Bear Aug 14 '18 at 13:30
  • You want to do this in three steps: create a dictionary, display it to the user, and then lookup their input in that dictionary to get their choice. – Patrick Haugh Aug 14 '18 at 13:30
  • @PatrickHaugh you don't need a dict here - a list allow indexed access. – bruno desthuilliers Aug 14 '18 at 13:33
  • Hello and welcome to the site! I've gone ahead and tidied up the language in your post, feel free to edit it again if you aren't happy with my changes. – MackM Aug 14 '18 at 13:33

3 Answers3

1

You can use random.sample to get a subset from your original list.

Then you can use enumerate() to number them, and input to ask for input.

import random

all_choices = ["thing1", "thing2", "thing3", "thing4", "thing5"]

n_choices = random.randint(1, 3)
subset_choices = random.sample(all_choices, n_choices)


for i, choice in enumerate(subset_choices, 1):
    print(i, "-", choice)

choice_num = 0
while not (1 <= choice_num <= len(subset_choices)):
    choice_num = int(
        input("Choose (%d-%d):" % (1, len(subset_choices)))
    )

choice = subset_choices[choice_num - 1]

print("You chose", choice)
AKX
  • 152,115
  • 15
  • 115
  • 172
0

You can first shuffle the list, then assign a number for each item in the list to a dictionary:

from random import shuffle

random_dict = {}
list = ['thing1', 'thing2', 'thing3', 'thing4', 'thing5']

shuffle(list)

for number, item in enumerate(list):
    random_dict[number] = item

Same code using a dictionary comprehension:

from random import shuffle

list = ['thing1', 'thing2', 'thing3', 'thing4', 'thing5']
shuffle(list)
random_dict = {number: item for number, item in enumerate(list)}

Then you have a dictionary with keys from 0 (if you want to start enumeration from 1, just set enumerate(list, start=1)) and randomly ordered the items from the list.

The dictionary itself is not really necessary, because every item in the shuffled list has already a position. But I recommend it anyway, it is a no-brainer.

You can then use the dict like this:

for k, v in random_dict.items():
    print("{} - {}".format(k, v))

decision = int(input("Which one do you choose? "))
print(random_dict[decision])
colidyre
  • 4,170
  • 12
  • 37
  • 53
0

If I'm understanding correctly, your main issue is listing all the items in the list correct?

To easily display all the items in the list and then respond with what they've picked, this code should work.

list = ['thing1', 'thing2', 'thing3', 'thing4', 'thing5']
for i in range(len(list)):
    print(str(i)+": "+list[i])
UI = input("Make a selection: ")
print("You selected: "+list[int(UI)])

or change that last print statement to whatever you need the program to do with the user input UI.

J0hn
  • 570
  • 4
  • 19