2
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.

1 Answer 1

2

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])
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting a syntax error on my line 13 item1, item2, item3, item4, item5 = items.split(',') Is that incorrect?
You mean the line: chorol []? I think it probably should be chorol = [] ... But I think your code has more problems than just that ...
Haha, you're probably right with the last part there! I may have got a bit ahead of myself but learning through failure is kind of fun. I changed my chorol to items to match what you posted so I don't think it's that. Probably time to go do some reading on dictionaries, thanks for your help.
@user2023827 -- no problem. As a side note, the dict I have above is more often written as : d = {'sup':['eyes','nose','mouth'],'car':[...], 'tan':[...]}. I'm not sure why I wrote it the way I did ... Just wanted a change I guess :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.