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)

我试图让用户从一组 3 个列表中选择 2 个列表,然后从用户选择的 2 个列表中的每个列表中打印 1 个随机项目。我最近才开始学习编码,所以这个错误可能是非常基本的。

4

1 回答 1

2

您最好在这里使用字典:

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])
于 2013-01-30T04:00:41.013 回答