user2023827
user2023827

Reputation: 21

Pulling random item from user input list in python

sup = ['eyes', 'nose', 'mouth'] 
car = ['4wd', 'hatch', 'coupe']
tan = ['dark', 'light', 'pale']

objects = ['sup', 'car', 'tan']

import random

print 'choose 2 objects from the following list separated by commas:'
print 'sup, car, tan'

chorol []
chorol.extend (objects[:2])

from random import choice
print choice (1)

from random import choice
print choice (2)

I'm trying to get a user to pick 2 lists from a set of 3 and then print 1 random item from each of the 2 lists the user has chosen. I've only started learning to code very recently, so the mistake is probably very basic.

Upvotes: 2

Views: 1569

Answers (1)

mgilson
mgilson

Reputation: 309929

You're better off with a dictionary here:

import random
d = dict(sup = ['eyes', 'nose', 'mouth'],
         car = ['4wd', 'hatch', 'coupe'],
         tan = ['dark', 'light', 'pale'])

items = raw_input("pick 2 (separated by comma):" + ','.join(d)+' > ')
#The next line of code could be
#item1,item2 = [x.strip() for x in items.split(',')] 
#if you want to do some validation
item1, item2 = items.split(',')  

print 'picked:', random.choice(d[item1])
print 'picked:', random.choice(d[item2])

Upvotes: 2

Related Questions