我制作了一个程序来模拟球队之间的整个足球赛季。用户输入团队的名称和技能等级。然后使用泊松分布来比较他们的技术等级,并计算出两队之间的结果。每场比赛结束后,相关的名单都会被更新:获胜的球队获得3分(所以如果是第三支球队赢了,那么指数2的数值就会增加3)。我有一份关于得分、进球、失球、赢得比赛、抽签和输掉比赛的单独清单(附带说明--有没有更有效的方法来做到这一点?)我在赛季结束时遇到的问题是:每支球队的数据都是按照最初的输入顺序输出的。这是通过这样一个事实完成的:“名称”列表中的团队名称与他们在“点数”列表中的点数是相同的索引。所以问题是,如果我排序‘点’列表,那么他们将与他们的名字不同步。我希望这是有意义的,但下面是一个赛季的一个例子:
Enter number of teams in league: 4
Enter team 1 name: a
Enter team 2 name: b
Enter team 3 name: c
Enter team 4 name: d
Enter a skill: 1
Enter b skill: 3
Enter c skill: 5
Enter d skill: 8
===========================================
a's home games:
===========================================
a 2 - 0 b
a 0 - 2 c
a 0 - 0 d
===========================================
b's home games:
===========================================
b 2 - 3 a
b 1 - 0 c
b 0 - 0 d
===========================================
c's home games:
===========================================
c 1 - 0 a
c 1 - 0 b
c 0 - 1 d
===========================================
d's home games:
===========================================
d 4 - 0 a
d 2 - 0 b
d 0 - 0 c
Final table:
a Skill: 1 Points: 7 For: 5 Against: 9 Goal difference: -4 Wins: 2 Draws: 1 Losses: 3
b Skill: 3 Points: 4 For: 3 Against: 8 Goal difference: -5 Wins: 1 Draws: 1 Losses: 4
c Skill: 5 Points: 10 For: 4 Against: 2 Goal difference: 2 Wins: 3 Draws: 1 Losses: 2
d Skill: 8 Points: 12 For: 7 Against: 0 Goal difference: 7 Wins: 3 Draws: 3 Losses: 0
[4, 7, 10, 12]所以我现在要做的是能够按降分顺序打印最终的排名表,而不是按索引顺序打印。
很抱歉,如果这是糟糕的措辞-我的程序的代码可能更有用,如下所示:
import math
import random
#Lambda value in Poisson distribution for higher rated team
lambOne = 1.148698355
#Lambda value for lower rated team
lambTwo = 0.8705505633
#Poisson distribution calculating goals scored by the home team
def homeMatch(homeRating,awayRating):
global lambOne
global x
global y
if x == y:
raise ValueError
else:
lamb = lambOne**(int(homeRating)-int(awayRating))
homeScore = 0
z = random.random()
while z > 0:
z = z - ((lamb**homeScore * math.exp(lamb * -1))/(math.factorial(homeScore)))
homeScore += 1
return (homeScore-1)
#Poisson distribution calculating goals scored by away team
def awayMatch(homeRating,awayRating):
global lambTwo
global x
global y
#This check is to stop a team playing itself
if x == y:
raise ValueError
else:
lamb = lambTwo**(int(homeRating)-int(awayRating))
awayScore = 0
z = random.random()
while z > 0:
z = z - ((lamb**awayScore * math.exp(lamb * -1))/(math.factorial(awayScore)))
awayScore += 1
return (awayScore-1)
#Selecting number of teams in league
leagueSize = int(input("Enter number of teams in league: "))
#Initialising empty lists
teamNames = []
teamSkill = []
teamPoints = []
teamFor = []
teamAgainst = []
teamWins = []
teamDraws = []
teamLosses = []
#Populating lists with number of zeroes equal to the number of teams (one zero for each)
for x in range(leagueSize):
teamPoints += [0]
teamFor += [0]
teamAgainst += [0]
teamWins += [0]
teamDraws += [0]
teamLosses += [0]
#Entering names and skill ratings for each team
for i in range(leagueSize):
teamNames += [input("Enter team "+str(i+1)+" name: ")]
for j in range(leagueSize):
teamSkill += [input("Enter "+teamNames[j]+" skill: ")]
#Initialising variables
homeScore = 0
awayScore = 0
#The season begins - each team plays all of its home games in one go
for x in range(leagueSize):
#input("Press enter to continue ")
print("===========================================")
print(teamNames[x]+"'s home games: ")
print("===========================================\n")
for y in range(leagueSize):
error = 0
try:
homeScore = homeMatch(teamSkill[x],teamSkill[y])
#Skipping a game to stop a team playing itself
except ValueError:
pass
error += 1
try:
awayScore = awayMatch(teamSkill[x],teamSkill[y])
except ValueError:
pass
if error == 0:
#Updating lists
print(teamNames[x],homeScore,"-",awayScore,teamNames[y],"\n")
teamFor[x] += homeScore
teamFor[y] += awayScore
teamAgainst[x] += awayScore
teamAgainst[y] += homeScore
if homeScore > awayScore:
teamWins[x] += 1
teamLosses[y] += 1
teamPoints[x] += 3
elif homeScore == awayScore:
teamDraws[x] += 1
teamDraws[y] += 1
teamPoints[x] += 1
teamPoints[y] += 1
else:
teamWins[y] += 1
teamLosses[x] += 1
teamPoints[y] += 3
else:
pass
#Printing table (unsorted)
print("Final table: ")
for x in range(leagueSize):
#Lots of formatting
print(teamNames[x]+(15-len(teamNames[x]))*" "+" Skill: "+str(teamSkill[x])+(5-len(str(teamSkill[x])))*" "+" Points: "+str(teamPoints[x])+(5-len(str(teamPoints[x])))*" "+" For: "+str(teamFor[x])+(5-len(str(teamFor[x])))*" "+" Against: "+str(teamAgainst[x])+(5-len(str(teamPoints[x])))*" "+" Goal difference: "+str(teamFor[x]-teamAgainst[x])+(5-len(str(teamFor[x]-teamAgainst[x])))*" "+" Wins: "+str(teamWins[x])+(5-len(str(teamWins[x])))*" "+" Draws: "+str(teamDraws[x])+(5-len(str(teamDraws[x])))*" "+" Losses: "+str(teamLosses[x])+(5-len(str(teamLosses[x])))*" ")
teamPoints.sort()
print(teamPoints)很抱歉,这是非常长的,很可能是糟糕的措辞和低效,但我希望有人能够帮助我!(非常感谢:)
发布于 2016-09-15 12:28:07
虽然您当前的方法(几乎不可行),但它使您很难(例如)更改您想要存储的关于每个团队的信息。您可以考虑定义一个team类,它的每个实例都存储了有关特定团队的所有信息。
class Team:
def __init__(self, name, skill):
self.name = name
self.skill = skill
self.points = self.goals_for = self.goals_against = \
self.wins = self.draws = self.losses = 0这样可以通过传递名称和技能级别来创建新的team对象,方法如下:
t1 = Team("Bradford City", 3)t1现在有了带有给定值的属性name和skill,以及许多其他属性(points、goals_for等),它们的值都为零。
然后你就可以很容易地初始化联盟了:
league_size = 4
teams = []
for _ in range(league_size):
teams.append(Team(input("Name of team "+str(_)+": "),
int(input("Team "+str(_)+"'s skill level: ")))然后,要打印每个团队的技能级别,您可以遍历列表:
for team in teams:
print(team.name, team.skill)我希望这能让你对如何简化你的方法有所了解。您的比赛函数现在也可以将球队作为参数,并根据计算的结果直接修改team对象。
为了得到你想要的答案,一旦你有了一个团队的列表,你就可以根据他们轻松掌握的点数来列出他们:
for team in sorted(teams, key=lambda t: t.points):
print(team.name, team.skill, team.points, ...)据我所见,您的global声明没有必要(如果名称没有在本地定义,Python将查找满足引用的全局名称)。此外,函数的输入通常应该作为参数传递,仅仅从环境中获取东西是很糟糕的做法!
我希望这足以让您重新设计您的程序,使其更易于操作。作为一个初学者,我想说,你做得非常好,以达到这一步。接下来的步骤将是令你兴奋的!
后来添加了:您的所有操作都可能因此更容易编程:
for home in teams:
for away in teams:
if home is away: # Teams don't play themselves
continue
play_match(home, away)play_match函数将模拟比赛并调整每个队的统计数据。当然,你可以用另一行的读数来模拟客场比赛。
play_match(away, home)虽然我不确定你的算法在这方面是对称的。
https://stackoverflow.com/questions/39510399
复制相似问题