-3

hi im creating a tiny lotto program and the program needs to sort out the first 6 numbers of the dictionary using insertion sort and the last 2 using selection sort i have done the code and complied it but it give me the below set of result, the last list keeps repeating instead of it sorting it out and printing it can i know why and how to fix it

Player 1 has numbers [17, 21, 0, 3, 25, 22, 4, 10]
Player 2 has numbers [13, 22, 11, 20, 24, 28, 20, 7]
Player 3 has numbers [8, 11, 16, 22, 14, 9, 11, 1]
Player 4 has numbers [8, 23, 28, 30, 6, 19, 12, 24]
Player 5 has numbers [22, 10, 2, 13, 4, 7, 15, 27]
Player 6 has numbers [5, 20, 10, 11, 5, 2, 27, 13]
Player 7 has numbers [25, 9, 22, 5, 14, 8, 25, 20]
Player 8 has numbers [16, 19, 25, 6, 17, 18, 13, 13]
Player 9 has numbers [8, 18, 16, 8, 1, 14, 4, 8]
Sorted array is:
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
[8, 18, 16, 8, 1, 14, 4, 8]
the 8 winning numbers are
[9, 12, 5, 17, 14, 17] [16, 4]
import random

def random_generator():
    randomNumber = random.randint(0,30)
    return randomNumber

def winningNumbers():
    winningPwn = []
    winningSwn = []
    for currentPwn in range(6):
        randomPWN = random_generator()
        winningPwn.append(randomPWN)
    for currentSwn in range(2):
        randomSwn = random_generator()
        winningSwn.append(randomSwn)

    return winningPwn, winningSwn

def printWinningNumber(lotteryNumbers):
    for currentLotteryIndex in range(len(lotteryNumbers)):
        print(lotteryNumbers[currentLotteryIndex],end = " ")
    

def draw():
    winning = winningNumbers()
    print("the 8 winning numbers are")
    printWinningNumber(winning)
def generatePlayers():
    players = {}

    for player_id in range(1, 10):
        player_list = []
        for i in range(8):
            player_list.append(random_generator())
        players[player_id] = player_list

    for player_id in players:
       print("Player {} has numbers {}".format(player_id, players[player_id]))
    insertionsort(players[player_id])
    print ("Sorted array is:") 
    for i in players[player_id]: 
        print (players[player_id])
    return players
def insertionsort(players):
       for i in range(1, len(players)): 
  
        value = players[i] 
  
       
        
        while i >0 :
            if value < players[i]:
                players[i+1] = players[i]
                players[i] = value
                i = i - 1
            else:
                break
                
    
generatePlayers()
draw()
lokki
  • 1
  • 1
    Hint: Double check your print code: `for i in players[player_id]: print (players[player_id])` – Phu Ngo Sep 25 '20 at 16:47
  • [https://stackoverflow.com/questions/64058935/gives-out-key-error-0-when-using-insertion-sort-in-python](https://stackoverflow.com/questions/64058935/gives-out-key-error-0-when-using-insertion-sort-in-python) – Carlo Zanocco Sep 25 '20 at 16:47
  • your `insertionsort()` is never going to do anything. It will break out of the `while` loop right away because you set `value = players[i]` and then do, in effect `if value >= players[i]: break`. – CryptoFool Sep 25 '20 at 17:08
  • Have you done any debugging? Please see [ask], [help/on-topic]. – AMC Sep 25 '20 at 21:13

1 Answers1

0

Seems that most of your problem is your sort function. I tweaked a few other issues, and ended up with this:

import random


def random_generator():
    randomNumber = random.randint(0, 30)
    return randomNumber


def winningNumbers():
    winningPwn = []
    winningSwn = []
    for currentPwn in range(6):
        randomPWN = random_generator()
        winningPwn.append(randomPWN)
    for currentSwn in range(2):
        randomSwn = random_generator()
        winningSwn.append(randomSwn)

    return winningPwn, winningSwn


def printWinningNumber(lotteryNumbers):
    for currentLotteryIndex in range(len(lotteryNumbers)):
        print(lotteryNumbers[currentLotteryIndex], end=" ")


def draw():
    winning = winningNumbers()
    print("the 8 winning numbers are")
    printWinningNumber(winning)


def generatePlayers():
    players = {}

    for player_id in range(1, 10):
        player_list = []
        for i in range(8):
            player_list.append(random_generator())
        players[player_id] = player_list

    for player_id in players:
        print("Player {} has numbers {}".format(player_id, players[player_id]))
        insertionsort(players[player_id])
        print("     Sorted array is {}".format(players[player_id]))

    return players


def insertionsort(players):
    for i in range(1, len(players)):
        value = players[i]
        while i > 0:
            if value >= players[i-1]:
                break
            players[i] = players[i-1]
            i -= 1
        players[i] = value


generatePlayers()
draw()

Which produces this:

Player 1 has numbers [21, 26, 9, 14, 7, 11, 24, 19]
     Sorted array is [7, 9, 11, 14, 19, 21, 24, 26]
Player 2 has numbers [22, 10, 28, 23, 2, 25, 14, 21]
     Sorted array is [2, 10, 14, 21, 22, 23, 25, 28]
Player 3 has numbers [18, 4, 5, 17, 24, 7, 16, 28]
     Sorted array is [4, 5, 7, 16, 17, 18, 24, 28]
Player 4 has numbers [4, 8, 13, 5, 16, 7, 14, 23]
     Sorted array is [4, 5, 7, 8, 13, 14, 16, 23]
Player 5 has numbers [19, 17, 26, 19, 24, 29, 24, 2]
     Sorted array is [2, 17, 19, 19, 24, 24, 26, 29]
Player 6 has numbers [29, 7, 9, 18, 1, 7, 16, 21]
     Sorted array is [1, 7, 7, 9, 16, 18, 21, 29]
Player 7 has numbers [26, 12, 19, 5, 30, 19, 6, 21]
     Sorted array is [5, 6, 12, 19, 19, 21, 26, 30]
Player 8 has numbers [2, 9, 3, 0, 7, 7, 17, 10]
     Sorted array is [0, 2, 3, 7, 7, 9, 10, 17]
Player 9 has numbers [29, 28, 25, 24, 1, 23, 6, 27]
     Sorted array is [1, 6, 23, 24, 25, 27, 28, 29]
the 8 winning numbers are
[10, 23, 1, 30, 21, 9] [19, 1] 

One thing I notice is that the numbers can and do repeat in a single sequence. Lottery numbers don't work that way, right?

Good luck finishing your program.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44