定义draw_histogram()函数,该函数通过Python字典作为参数传递

时间:2018-10-06 10:18:34

标签: python

运行我的代码时,它始终不显示任何内容。我很困惑我的代码有什么问题。这是我编写的代码:

def draw_histogram(histogram_dict):
    all_keys = list(histogram_dict.keys())
    all_keys.sort()
    for key in all_keys:
       stars = "*" * histogram_dict[key] 
       print(key, ":", stars)
def test_draw_histogram():
    print("1.")
    draw_histogram({'a': 2, 'c': 7, 'b': 5})
    print()

    print("2.")
    draw_histogram({'a': 0, 'c': 5, 'b': 7, 'f': 0})

输出应该是这样的:

 a: **

 b: *****

 c: *******

1 个答案:

答案 0 :(得分:0)

def draw_histogram(histogram_dict):
    all_keys = list(histogram_dict.keys())
    all_keys.sort()
    for key in all_keys:
        stars = "*" * histogram_dict[key]
        print(key, ":", stars)

def test_draw_histogram():
    print("1.")
    draw_histogram({'a': 2, 'c': 7, 'b': 5})
    print("\n")
    print("2.")
    draw_histogram({'a': 0, 'c': 5, 'b': 7, 'f': 0})

test_draw_histogram()

您需要调用刚刚定义的函数。