《Python入门经典》的四分卫传球率算法

              四分卫(Quarterback)是美式橄榄球队进攻的核心队员,负责率队贯彻教练意图,完成指定战术。四分卫水平的好坏、传球成功率的高低,直接影响比赛的结果。
              在本书第1章习题32和第2章习题18都是关于美式足球四分卫传球率的算法实现。但是第1章习题32关于传球得分率的算法表述不太正确。 

             要计算传球手的得分率,应该是需要输入一下五个值

            COMP = Number of completions(传球完成次数)

            ATT = Number of passing attempts(传球总次数)
            YARDS = Passing yards(传球码数)
            TD = Touchdown passes(传球达阵次数)
            INT = Interceptions(传球被抄截次数)
           计算公式:

                 C是"每次传球的成功率" 乘以100,减去30,然后除以20

            Y是"每次传球码数"减3,除以4

            T是"每次传球达阵率"乘以20

             I是2.375减去("每次传球被拦截率"乘以25) [书本在这里错误!]

             传球得分率是(C+Y+T+I)/ 6 * 100

                                          《Python入门经典》的四分卫传球率算法_第1张图片

             限制条件:

             (当C、Y、T、I四项中任意一个小于0或大于2.375时,将对应项的值替换为0或2.375进行计算)PS:这个在《python入门经典》中没有提及到,在此算法中也就不考虑了!但是在真实的传球率计算中是有相关规定的。

             代码解决是采用了python中的字典。(新手,代码写得有点渣,将就着看吧,不足之处,还请留言交流)

 

# -*- coding: cp936 -*-
dataValue = {}  #dictionary for athlete data
count = 1
print "输入运动员信息,以null结束!:"
while True:
    print "Number:", count
    name = raw_input("Enter Name:")
    if name == "null":
        break
    completionPass = int(raw_input("Enter number of success Passes:"))
    numberPass = int(raw_input("Enter number of passes:"))
    totalYards = int(raw_input("Enter total yards:"))
    touchdownTimes = int(raw_input("Enter touchdown times:"))
    interceptTimes = int(raw_input("Enter intercept times:"))
    dataValue.setdefault(name,[completionPass,numberPass,totalYards,touchdownTimes,interceptTimes])
    count = count + 1
    print "\n",
scoreRateDic = {} #dictionary for score rate
for key,val in dataValue.items(): #读取字典中的输入数据,计算每名运动员传球得分率
    eachSucPassRate = float(val[0]) / float(val[1])
    eachYard = float(val[2]) / float(val[1])
    eachTouchdownRate = float(val[3]) / float(val[1])
    eachInterceotRate = float(val[4]) / float(val[1]) 
    C = ((eachSucPassRate * 100) - 30) / 20
    Y = (eachYard - 3) * 0.25
    T = eachTouchdownRate * 20
    I = 2.375 - (eachInterceotRate * 25)
    scoreRate = (C + Y + T + I) / 6 * 100
    scoreRateDic.setdefault(key,scoreRate)
for key,val in scoreRateDic.items(): #评级功能
    if val > 95:
        print key,":",val,":Great Athlete!"
    elif 90 < val <=95:
        print key,":",val,":Good Athlete!"
    elif 85 < val <=90:
        print key,":",val,":Ordinary Athlete!"
    else:
        print key,":",val,":Bad Athlete!"

             验证结果:

            分享一个四分卫的实际数据地址:http://www.nfl.com/stats/categorystats?archive=false&conference=null&statisticCategory=PASSING&season=2014&seasonType=REG&experience=&tabSeq=0&qualified=false&Submit=Go

              《Python入门经典》的四分卫传球率算法_第2张图片

           程序执行结果:

《Python入门经典》的四分卫传球率算法_第3张图片

资源来源:

谢谢!

1、大神关于四分卫传球率算法的研究http://www.tdl100.com/thread-27843-1-1.html

2、维基百科关于四分卫传球率算法介绍http://en.wikipedia.org/wiki/Passer_rating


PALE




你可能感兴趣的:(Python修行)