[英]Python - 5 random items from user chosen lists in dictionary
我正在嘗試從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(',')
編輯:修復錯誤
您在末尾缺少親密伙伴
items = raw_input("Pick 5 and separate them by a comma:" + ','.join(d)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.