【问题标题】:Write a function that accepts a dictionary as parameter and prints key-values where the key is divisible by 2编写一个函数,接受字典作为参数并打印键值可被 2 整除的键值
【发布时间】:2022-10-01 10:46:06
【问题描述】:
dictionary={\'a\':2, \'b\':3, \'c\':4, \'d\':5}
def print_events(d):
    for key in d:
        if key.values()%2==0:
            print(d)
    
print_events(dictionary)

这是我为它写的,我不知道如何解决这个问题。所以我想要的答案是\'a\':2 \'c\':4。只需打印出偶数。

  • 你想要价值能被 2 整除,而不是关键。键是\'a\'、\'b\'、\'c\'、\'d\'。

标签: python function loops dictionary tuples


【解决方案1】:

这是对您的代码的修复。

dictionary={'a':2, 'b':3, 'c':4, 'd':5}
def print_events(d):
    for key,value in d.items():
        if value%2==0:
            print(f"{key}:{value}")
    
print_events(dictionary)

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多