简体   繁体   中英

Choosing from a randomly generated list - python

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*

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)

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])

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM