Python - 用户在字典中选择列表中的5个随机项

时间:2013-01-30 05:29:00

标签: python list printing dictionary user-input

我正在尝试从5个不同的用户选择列表中打印一个号码 这是代码

import random

d = {'january':['1', '2', '3', '4', '5', '6', '7']
,'febuary':['1', '2', '3', '4', '5', '6', '7']
,'march':['1', '2', '3', '4', '5', '6', '7']
,'april':['1', '2', '3', '4', '5', '6', '7']
,'may':['1', '2', '3', '4', '5', '6', '7']
,'june':['1', '2', '3', '4', '5', '6', '7']
,'july':['1', '2', '3', '4', '5', '6', '7']
,'august':['1', '2', '3', '4', '5', '6', '7']}

items = raw_input("Pick 5 and separate them by a comma:" + ','.join(d)
item1, item2, item3, item4, item5 = items.split(',')

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

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

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

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

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

raw_input ()

我一直在

SyntaxError on line 13;
    item1, item2, item3, item4, item5 = items.split(',')

我也试过

items = raw_input("Pick 5 separated by comma:" + ','.join(d)
splitItems = items.split(',')

#print roles

print 'picked:', random.choice(d[splitItems[0]])
print 'picked:', random.choice(d[splitItems[1]])
print 'picked:', random.choice(d[splitItems[2]])
print 'picked:', random.choice(d[splitItems[3]])
print 'picked:', random.choice(d[splitItems[4]])

还有一个

SyntaxError on line 13;
    splitItems = items.split(',')

编辑:修正错误

1 个答案:

答案 0 :(得分:0)

你错过了

末尾的密切关注
items = raw_input("Pick 5 and separate them by a comma:" + ','.join(d)