【问题标题】:Python - 5 random items from user chosen lists in dictionaryPython - 字典中用户选择列表中的 5 个随机项目
【发布时间】:2013-01-30 05:29:00
【问题描述】:

我正在尝试从 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(',')

编辑:修正错误

【问题讨论】:

  • 您在 raw_input 行上缺少右括号。这是一个常见问题——当你得到一个没有意义的 SyntaxError 时,请始终查看上面的行——很可能你没有关闭括号或 {[
  • 看来我的回答仅基于typo
  • !!完美,非常感谢您的回答以及今天早些时候的回答!终于搞定了。再次感谢

标签: python list printing dictionary user-input


【解决方案1】:

你在结尾处缺少一个近括号

items = raw_input("Pick 5 and separate them by a comma:" + ','.join(d)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多