【问题标题】:Running dict.clear but the dictionary still prints with items运行 dict.clear 但字典仍然打印项目
【发布时间】:2016-06-14 23:19:58
【问题描述】:

我正在尝试运行选项 11 来清除我的字典,但是当我运行清除选项 (11) 和查看所有项目选项 (4) 时,它会打印与上面相同的字典而不清除它。

有什么想法可以让我在遇到困难时让它工作吗?

basketball_scores = {'Luke' : 18, 'Nick' : 7, 'Ben': 10, 'Dylan' : 87, 'Liam' : 34, 'Jake' : 69,
                     'Salmon' : 2, 'Ashley' : 120, 'Kirkus' : 1, 'Parlas' : -9}

def menu():
    print('''
=====Player Points Dictionary=====
    1. Add a player.
    2. Delete a player.
    3. Modify a player's points.
    4. View all items.
    5. Get a player's statistic.
    6. View all player's.
    7. View all point statistics.
    8. Sort dictionary by player name.
    9. Sort dictionary by point statistic.
    10. Player lucky dip.
    11. Clear the dictionary.
    12. Exit.
==================================
''')

def main():
    menu()
    option = int(input('Which option would you like to run? '))
    print()

    while option not in range(1, 11):
        option = int(input('Which option would you like to run? '))
        print()

    if option in range(1, 11):
        if option == 1:
            name = str(input("What is the player's name? "))
            points = int(input('How many points has {0} scored? '.format(name)))
            basketball_scores[name] = points
        elif option == 2:
            name = str(input('Which player would you like to delete? '))
            del basketball_scores[name]
        elif option == 3:
            name = str(input("Which player's points would you like to change? "))
            points = int(input('How many points has {0} scored? '.format(name)))
            basketball_scores[name] = points
        elif option == 4:
            print(dict.items(basketball_scores))
        elif option == 5:
            name = str(input("Which player's points would you like? "))
            print('{0} has scored {1} points.'.format(name, basketball_scores.get(name)))
        elif option == 6:
            print(dict.keys(basketball_scores))
        elif option == 7:
            print(dict.values(basketball_scores))
        elif option == 8:
            # A dictionary has absolutely no order, the sorted dictionary is really an array with lists inside of it
            # t[0] is the first item in the dictionary, so a player name. Therefore it will sort by name/string
            sortedByNameDict = sorted(basketball_scores.items(), key = lambda t: t[0])
            print(sortedByNameDict)
        elif option == 9:
            # t[1] is the second item in the dictionary, so a player's points. Therefore it will sort by points/integer.
            sortedByPointsDict = sorted(basketball_scores.items(), key = lambda t: t[1])
            # dictionary.reverse simply reverses the list so that it is in descending points order (largest to smallest)
            sortedByPointsDict.reverse()
            print(sortedByPointsDict)
        elif option == 10:
            print(dict.popitem(basketball_scores))
        elif option == 11:
            dict.clear(basketball_scores)
        elif option == 12:
            quit

main()

【问题讨论】:

  • 为什么要调用dict.... 而不是直接在basketball_scores 字典上调用方法?范围也是半开的,所以你永远不会达到 11
  • 此外,如果您的 wile 循环中断,则选项必须在该范围内,因此您的 if 是多余的。
  • x in range(a,b) 表示 a

标签: python python-3.x dictionary


【解决方案1】:

range(1, 11) 不包括 11,因此您只是跳过了该输入。我建议使用链式比较:

1 <= option <= 11

而不是in range(...)。它使边界上的行为更加明确(并允许您指定行为),它在 Python 2 上效果更好,并且适用于浮点数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2019-03-31
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多