我创建了一个模拟团队之间整个足球赛季的计划。用户输入团队'名字和他们的技能等级。然后,它使用泊松分布来比较他们的技能等级并计算两个团队之间的结果。每场比赛结束后,相关名单都会更新:获胜队伍获得3分(因此,如果是第三支球队获胜,则指数[2]的值增加3分)。我有一个单独的积分列表,得分,失球,赢得的比赛,抽取的比赛和失去的比赛(旁注 - 有更有效的方法吗?) 我遇到的问题是在赛季结束时:每支球队都按照球队最初输入的顺序输出他们的数据。这是通过使用'名称中的团队名称来实现的。 list与他们在'点中的点数相同的索引。名单。所以问题是,如果我点了点'#39;列表然后他们将与他们的名字不同步。我希望这是有道理的,但这是一个季节的示例输出:
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)
对不起,这很长,可能措辞不好,效率低但我希望有人能帮助我!非常感谢你:))
答案 0 :(得分:3)
虽然您目前的方法(几乎)不可行,但却很难(例如)更改您想要存储的关于每个团队的信息。您可以考虑定义一个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
这使您可以通过传递名称和技能级别来创建新的团队对象:
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)
全部为零。
然后你可以很容易地初始化联盟:
for team in sorted(teams, key=lambda t: t.points):
print(team.name, team.skill, team.points, ...)
然后打印每个团队的技能等级,你可以循环遍历列表:
global
我希望这可以让您了解如何简化您的方法。您玩游戏的功能现在也可以将团队作为参数,并根据计算结果直接修改团队对象。
要获得你想要的答案,一旦你有一个团队列表,你可以很容易地打印出来,按照他们持有的点数排序:
for home in teams:
for away in teams:
if home is away: # Teams don't play themselves
continue
play_match(home, away)
据我所知,您的play_match
声明都不是必需的(如果名称未在本地定义,Python将查找全局名称以满足引用)。除此之外,函数的输入通常应该作为参数传递,从环境中获取东西是相当糟糕的做法!
我希望这足以让您重新编写程序以使其更容易处理。作为初学者,我说你已经做得非常好了。接下来的步骤对你来说将是令人兴奋的!
稍后添加:您的全部播放可能更容易编程:
play_match(away, home)
<!DOCTYPE html>
<html>
<body>
<h1>Radio Buttons </h1>
<div id = "demo"> </div>
<br>
<button onclick="myFunction()"> add </button>
<script>
var x=0;
var y=0;
function myFunction() {
var html="";
for(var i=0;i<x;i++)
{
if(document.getElementById("radio-"+i).checked)
html=html+"<input type=radio id=radio-"+i+" name=rad-"+i+" checked> Yes <input type=radio id=radio1-"+i+" name=rad-"+i+"> No <br>";
else
html=html+"<input type=radio id=radio-"+i+" name=rad-"+i+"> Yes <input type=radio id=radio1-"+i+" name=rad-"+i+" checked> No <br>";
}
html=html+"<input type=radio id=radio-"+y+" name=rad-"+x+"> Yes <input type=radio id=radio1-"+y+" name=rad-"+x+"> No <br>";
document.getElementById("demo").innerHTML=html;
x++;
y++;
}
</script>
</body>
</html>
功能会模拟匹配并调整每个团队的统计信息。当然你可以用另一条线读取
list.sort()
虽然我不确定你的算法是否对称。