试图在 python 中为我的程序创建排行榜

trying to create a leaderboard for my program in python

我正在做一项任务,目标是... 为学校锦标赛创建一个程序,参与者可以参加锦标赛,然后根据他们在比赛中的位置将积分分配给团队。会有诸如(躲避球、足球、数学、英语)之类的活动,但我会在以后处理这些活动

目前我正在尝试构建一些代码来为正在运行的团队分配一个点。然后打印排行榜(团队列表)这不起作用

leaderboard() 函数不起作用,我不知道为什么,请帮助我

class Team:
    def __init__(self, num, name, size, score):
        self.num = num
        self.name = name
        self.size = size
        self.score = score

    def add_victory(self):
        self.score += 1

    def __repr__(self):
        return f'Team Number: {self.num} |-| Team Name: {self.name} |-| Member Count: {self.size} |-| Team Score: {self.score}'

def NewTournament():
    teams = []

    TeamCounter=int(input('How many Teams will be in the tournament? '))

    print('')
    for i in range(TeamCounter):
        NameOfTeam=input(f'Please Enter Team {i+1} Name: ')
        MemberCount=int(input('How Many Members in Team? '))
        print('')
        teams.append( Team( i+1, NameOfTeam, MemberCount, 0) )


def Score(teams):
    winner = input('Which Team Won the Event? ')
    for team in teams:
        if team.name == winner:
            team.add_victory()
            break
            print('Updated Leaderboard')

def Leaderboard():
    for t in teams: #does not work, does nothing
        print(t)
    
def Menu():
    MenuLoop=1
    while MenuLoop==1:
        print('1.Create Tournament')
        print('2.Update Existing Tournament')
        print('3.View Leaderboard')
        print('4.Exit')
        MenuOption=input('')
        if MenuOption=='1':
            print('Creating Tournament')
            NewTournament()#runs the new tournament function
            MenuLoop-=1
            Menu()
        elif MenuOption=='2':
            print('Updating Tournament')
            MenuLoop-=1
            EventName=input('Which Event Was Completed? ')
            winner=input('Which Team Won the Event? ')
            print('Event Winner:', winner, '||', 'Event:',EventName)
            print('Is this correct? Y/N')
            Check=input('')
            if Check=='y':
                print('Updating Leaderboard')
                Score(teams)
                Menu()
                    
                
                
        elif MenuOption=='3':
            MenuLoop-=1
            Leaderboard()
                
        elif MenuOption=='4':
            print('Exiting Program...')
        else:
            print('Error, please choose an option from the list below.')#keeps looping if user is not choosing a correct number from list


#start of program
teams = []        

print('░██╗░░░░░░░██╗███████╗██╗░░░░░░█████╗░░█████╗░███╗░░░███╗███████╗')
print('░██║░░██╗░░██║██╔════╝██║░░░░░██╔══██╗██╔══██╗████╗░████║██╔════╝')
print('░╚██╗████╗██╔╝█████╗░░██║░░░░░██║░░╚═╝██║░░██║██╔████╔██║█████╗░░')
print('░░████╔═████║░██╔══╝░░██║░░░░░██║░░██╗██║░░██║██║╚██╔╝██║██╔══╝░░')
print('░░╚██╔╝░╚██╔╝░███████╗███████╗╚█████╔╝╚█████╔╝██║░╚═╝░██║███████╗')
print('░░░╚═╝░░░╚═╝░░╚══════╝╚══════╝░╚════╝░░╚════╝░╚═╝░░░░░╚═╝╚══════╝')#welcome user
print('')
Username=input('Enter Username: ')
Password=input('Enter Password: ')
if Username =='Admin' and Password == 'Admin':#very basic login system for security of school
    print('Logging in...')
    print('User Verified')
    Menu()
else:
    print('User Does Not Exist.')#stops pupils gaining unauthorised access

请理解,我对 python 还是很陌生,所以布局可能很乱或很复杂,甚至可能对您来说没有任何意义,我正在努力为您理解它

您的问题来自您如何存储您的团队。在底部,您将全局 teams 列表初始化为 teams = []。然后稍后在 NewTournament 中创建一个新的 teams 列表,它与全局列表冲突:

def NewTournament():
    teams = []

    TeamCounter=int(input('How many Teams will be in the tournament? '))

    print('')
    for i in range(TeamCounter):
        NameOfTeam=input(f'Please Enter Team {i+1} Name: ')
        MemberCount=int(input('How Many Members in Team? '))
        print('')
        teams.append( Team( i+1, NameOfTeam, MemberCount, 0) )

因此,当您将新的 Team 附加到 teams 时,更改不会更改您在列出排行榜时使用的全局 teams。最简单的解决方案是只删除本地 teams 变量:

def NewTournament():
    TeamCounter=int(input('How many Teams will be in the tournament? '))

    print('')
    for i in range(TeamCounter):
        NameOfTeam=input(f'Please Enter Team {i+1} Name: ')
        MemberCount=int(input('How Many Members in Team? '))
        print('')
        teams.append( Team( i+1, NameOfTeam, MemberCount, 0) )/

更好的解决方法是创建一个新的 Tournament class,它可以更清楚地存储锦标赛的状态(球队列表),并消除您对顶级方法的需求它依赖于全局变量,这很少是一个好主意。