m0_74939047 2025-04-25 15:31 采纳率: 0%
浏览 28

头歌Python基础综合测试第9关:美国费城Danny`s Wok中餐馆菜单分析—最佳购买方案

编程要求
在编辑器中编写代码补全showChikenLine(fileName)函数。绘制鸡翅单价是随着鸡翅数变化的折线图,折线的所有属性均为默认属性。

fileName 参数为菜单价格的txt文件。
你可以通过如下链接下载本关涉及到的数据文件用作本地调试:
menu.txt
Number of Chicken Wings, Price
4,4.55
5,5.7
6,6.8
7,7.95
8,9.1
9,10.2
10,11.35
11,12.5
12,13.6
13,14.75
14,15.9
15,17
16,18.15
17,19.3
18,20.4
19,21.55
20,22.7
21,23.8
22,24.95
23,26.1
24,27.25
25,27.8
26,28.95
27,30.1
28,31.2
29,32.35
30,33.5
35,39.15
40,44.8
45,50.5
50,55.6
60,67
70,78.3
75,83.45
80,89.1
90,100.45
100,111.25
125,139
150,166.85
200,222.5

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-04-25 15:31
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解答

    根据问题,需要编写一个 Python 函数 showChikenLine,用于绘制鸡翅单价折线图。函数参数 fileName 是菜单价格的 txt 文件。

    下面是解决方案的核心代码:

    import matplotlib.pyplot as plt
    
    def showChikenLine(fileName):
        # 读取txt文件
        with open(fileName, 'r') as f:
            lines = [line.strip().split(',') for line in f.readlines()]
        
        # 提取数据
        chicken_prices = [float(line[1]) for line in lines]
        chicken_counts = [int(line[0]) for line in lines]
        
        # 绘制折线图
        plt.plot(chicken_counts, chicken_prices)
        plt.xlabel('Chicken Count')
        plt.ylabel('Price')
        plt.title('Chicken Price vs Count')
        plt.show()
    

    这个函数首先读取 txt 文件,提取鸡翅数量和价格,然后使用 matplotlib 库绘制折线图。

    评论

报告相同问题?

问题事件

  • 创建了问题 4月25日