在为Win%排序数组时出现问题

时间:2018-12-04 22:02:05

标签: arrays python-3.x sorting

我一直在努力完成该项目的上课时间,除对获胜百分比进行排序外,我已经完成了所有工作。不幸的是,我原计划在学习这一部分的同一天参加CCNA考试,但我似乎无法理解这一概念。我的教授使用了一种将每个元素与最初设置为“ 0”的“ lowIndex”变量进行比较的方法。我似乎无法从他的示例中将其实现到问题中,但是似乎可以通过python的sort方法来完成。我的问题是让它真正起作用并正常运行。下面是我的“稳定代码”,但是我还有一个测试文件,我一直在其中工作,我在这里创建了一个新函数来进行排序,但是我也一无所获。本质上,我希望它按从高到低的顺序排序,但是我认为我需要将它从for循环中删除,但是当我这样做时,我会遇到更多问题,这使我不得不更多地屠杀我的代码。再次,我觉得现在已经成功(我相信)创建了一个percent []数组,.sort应该可以正常工作,实际上我在实现它时遇到了一个问题。我需要对其进行正确排序,以便当它在“ viewRecords”中显示时,将按从高到低的顺序列出。任何帮助将不胜感激。

    import random

def main():

    #declare 4 parallel arrays - Arrays will stay in main, but will pass to a function
    names = []
    wins = []
    losses = []
    skill = []
    percentage = []

    #Call loadPlayers to initialize array
    loadPlayers(names, wins, losses, skill, percentage)

    #put menu
    choice = menu()

    while choice != 6:

        #clear lines
        for i in range(30):
            print()

        if choice == 1:
            viewRecords(names, wins, losses, skill, percentage)


        elif choice == 2:
            simulateMatches(names, wins, losses, skill)

        elif choice == 3:
            changeSkill(names, skill)


        elif choice == 4:
            clearRecords(wins, losses)


        elif choice == 5:
            newPlayer(names, wins, losses, skill)

        else:
            print('Invalid Choice')
        #end if


        choice = menu()          
    #End While
#End Main



#Menu Function
def menu(): 
    print('''1. View Records
2. Simulate Matches
3. Change Player Skill
4. Clear Records
5. Add New Player
6. Exit
\n
''')
    choice = getRange('your choice', 1, 6)

    return choice
#End Menu Function








#Initialize Array
def loadPlayers(names, wins, losses, skill, percentage):
    names.append('Katelyn')
    names.append('David')
    names.append('Caleb')
    names.append('Syed')
    names.append('Zane')

    names.append('Ian')
    names.append('Chuck Norris')
    names.append('Michal')
    names.append('Dustin')
    names.append('Ashley')

    names.append('Luke')
    names.append('Michael')
    names.append('Aidan')
    names.append('Pavan')
    names.append('Angel')

    names.append('Ricky')
    names.append('Louis')
    names.append('Evan')
    names.append('Valdas')
    names.append('Jared')

    names.append('Thomas')
    names.append('Cam')

    # Loop and fill other 3 arrays
    numPlayers = len(names)

    #Zero out wins and losses and give a random skill rating 1-100
    for i in range(numPlayers):
        wins.append(0)      #Set wins/losses to 0
        losses.append(0)
                            #Set random skill rating 1-100
        skill.append(random.randint(1,100))

        #cheat code
        if names[i] == 'Dustin':
            skill[i] = 101
        elif names[i] == 'Chuck Norris':
            skill[i] = 2000
        #End if
    #End For


    #Set win % to 0
    for i in range(numPlayers):
        percentage.append(0)
    #End win % set to 0


    #End Init
#End loadPlayers






#Begin getRange
def getRange(msg, low, high):
    value = int(input('Please enter ' + msg + ': '))

    while value < low or value > high:
        print('Error, ' + msg + ' must be ' + str(low) + ' - ' + str(high))
        value = int(input('Try again: '))

    return value
#End getRange







#Begin View Records
def viewRecords(names, wins, losses, skill, percentage):
    print('View Records')
    print('\n\n')

    print('Names\t\tRecord\t\tSkill\t\tWin %')
    print('-----\t\t------\t\t-----\t\t---')

    for i in range(len(names)):

        #print name
        print(names[i], end='\t')

        #check for short names (if < 8 characters, give extra tab)
        if len(names[i]) < 8:
            print(end = '\t')

        #print record
        print('(' + str(wins[i]) + '-' + str(losses[i]) + ')', end='\t')

        #check for short records (4 digits or less) give extra tab
        if (len(str(wins[i])) + len(str(losses[i]))) <= 4:
            print(end='\t')

        #print skill
        print(skill[i], end ='\t\t')

        #print win%

        if wins[i] + losses[i] != 0:           
            percentage[i] = (wins[i] / (wins[i] + losses [i]) * 100)

##        percentage.sort <----Assuming this won't work because it's in the for loop, but not sure where to put it so that it sorts it,
            ##                  but will display within viewRecords correctly sorted

        print(format(percentage[i], '.1f') + '%')





        #End IF
    #End For

    print('\n')

#End viewRecords Function





#Start simulateMatches
def simulateMatches(names, wins, losses, skill):
    print('Simulate Matches')
    print('\n\n')

    for i in range(500):

        #pick 2 players
        player1 = random.randint(0, len(names) - 1)
        player2 = random.randint(0, len(names) - 1)

        #Code to avoid playing yourself
        while player2 == player1:
            player2 = random.randint(0, len(names) - 1)
        #end while

##          print(names[player1], skill[player1], 'vs. ', names[player2], skill[player2])

        combinedSkill = skill[player1] + skill[player2]

        winNumber = random.randint(0, combinedSkill -1)

        #check for winner/loser
        if winNumber < skill[player1]:
            #who wins
            wins[player1] += 1
            losses[player2] += 1
##              print(names[player1],'wins!')
##              input('Press any key to continue!')
        else:
            wins[player2] += 1
            losses[player1] += 1
##              print(names[player2], 'wins!')
##              input('Press any key to continue!')
        #end if


#Begin Change Skill
def changeSkill(names, skill):

    searchName = input('Enter the Player\'s name that you would like to change: ')

    for i in range(len(names)):
        if searchName == names[i]:
            newSkill = getRange('new skill', 1, 100)
            skill[i] = newSkill
#End Change Skill


















#End Change Skill




#Clear the records
def clearRecords(wins, losses):
    for i in range(len(wins)):
        wins[i] = 0
        losses[i] = 0

    print('Success! Records have been reset. ')
    print('\n\n')

    return wins, losses
#End Clear Records



#Add a new player
def newPlayer(names, wins, losses, skill):
    print('View Records')
    print('\n\n')
    newName = input('Enter new player\'s name: ')

    names.append(str(newName))
    wins.append(0)
    losses.append(0)
    skill.append(random.randint(0, 100)) #Seemed wrong to let you choose your skill, so I made it random
    print('\n\n')
#End New Player



main()

0 个答案:

没有答案