天天看點

Python每日一練(2)-模拟評委對歌手打分

在布塔學院的某次十佳歌手比賽中,7位評委對歌手打分,計算總分時,需要去掉一個最高分,去掉一個最低分,然後輸出總分和平均分。要求幫助評委會編寫歌手打分程式,輸入7名評委的打分,輸出總分和平均分。最終效果如圖所示。

Python每日一練(2)-模拟評委對歌手打分

示例代碼如下:

# -*- coding: utf-8 -*-
# @Time    : 2020/4/2 14:45
# @Author  : 我就是任性-Amo
# @FileName: 53.模拟評委對歌手打分.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/xw1680


if __name__ == '__main__':
    # 1.列印标題和分割線
    print("\033[1;35m  暢想布塔學院十佳歌手打分程式")
    print("===================================\033[0m")
    # 2.使用input函數錄入7名裁判的打分
    score_str = input("請輸入7名裁判的打分,用英文逗号間隔分數: \n")
    # 3.将分數使用,進行切割 得到一個清單
    # ['78.5', '67.2', '89', '98.7', '88', '99', '77']
    temp_score_list = score_str.split(",")
    # 注意: 此時清單中的分數仍是字元串 所有我們要借助map函數将清單中的每個元素轉換為數值型的資料
    # [78.5, 67.2, 89.0, 98.7, 88.0, 99.0, 77.0]
    score_list = list(map(float, temp_score_list))
    # 4.分别拿到最高分和最低分
    max_score = max(score_list)
    min_score = min(score_list)
    # 5.列印輸出
    print(f"去掉一個最低分: {min_score}")
    score_list.remove(min_score)
    print(f"去掉一個最高分: {max_score}")
    score_list.remove(max_score)
    # 輸出有效得分
    print(f"該歌手的有效打分為: {score_list}")
    # 輸出最後得分 最後得分保留一位小數
    print("該歌手的得分為: %.1f" % (sum(score_list) / len(score_list)))