【问题标题】:using dictionaries/classes in python 2.7在 python 2.7 中使用字典/类
【发布时间】:2016-01-18 07:05:44
【问题描述】:

嗨,我正在尝试创建一个程序,其中用户输入将存储在字典/类中(对于不确定术语来说真的很新)。

但基本上我想在我的代码开头有 3 个字典/类,它们将存储三个团队的信息,例如团队名称,然后在过去 3 周内获得了多少积分。

然后,用户将输入下一周的积分以存储到课程/字典中,而该课程/字典将依次删除前第 3 周。

Premiership = {
    "Arsenal": ['points': 23,20,19],
    "Chelsea": ['points': 15,14,11],
    "Man utd": ['points' : 20, 17, 14],
}

Championship = {
    "Derby county": ['points': 23,20,19],
    "QPR": ['points': 15,14,11],
    "Leeds Utd": ['points' : 20, 17, 14],
}

League one = {
    "Sheffield utd": ['points': 23,20,19],
    "Barnsley": ['points': 15,14,11],
    "Bradford city": ['points' : 20, 17, 14],
}

所以他们会通过以下方式选择联赛:

selection = raw_input('please choose a league: ')

然后,一旦他们完成了,我想允许用户更新分数并摆脱一个

【问题讨论】:

  • 不确定实际的问题是什么。无论如何,看起来你应该看看tutorials

标签: python dictionary store


【解决方案1】:

您的语法与字典的构建方式有些不同。您的 Sub 包含无效的语法列表,应该看起来更接近:

Premiership = {
    "Arsenal": {'points': [23,20,19]},
    "Chelsea": {'points': [15,14,11]},
    "Man utd": {'points' : [20, 17, 14]},
}

更好的方法可能是稍微更改格式,因为在当前情况下,“点”字段没有提供任何好处

Premiership = {
    "Arsenal": [23,20,19],
    "Chelsea": [15,14,11],
    "Man utd": [20, 17, 14],
}

您可以按如下方式访问英超词典中的内容

Premiership["Arsenal"][0] = <<new value>>

您将需要弄清楚如何轮换列表值的逻辑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多