Search code examples
pythonloopsaveragehead

I want to find average of this loop


import random
player = ['H','T']
player_win = []
for x in range(10):
    
    while True:
        random.shuffle(player)
        #print(player)
        if player[(0)] == 'H' :
            player_win.append(1)
            #print(player_win)
            continue
        else :
            player_win.append('LOST')
            #print('PLAYER LOST')
        break

I'm new in python and right now as a task I'm making a logic where I need to find the average of this loop so I could decide at what minimum value the game should be put so that the dealer is always in profit

Well actually this is my own logic where I don't know if I'm correct or not but overall I want that know at what price of the game I should set so I make profit price = ? player_price = 1 if win double the player_price
else 0 again ask to play or not if not break if yes again toss and now if win double the previous player_win else 0 So in the whole game find what the average price of the game should be and it should solely done by computer only (No, user input intended)


Solution

  • Instead of appending the string of 'LOST', instead append 0 as a loss, you can then get the average of the player_win list.

    import random
    player = ['H','T'] 
    player_win = [] 
    for x in range(10):
        while True:
            random.shuffle(player)
            #print(player)
            if player[(0)] == 'H' :
                player_win.append(1)
                print(player_win)
                continue
            else :
                player_win.append(0)
                print('PLAYER LOST')
            break
    # get the average of the list
    average = sum(player_win) / len(player_win)
    print(f"Average Wins: {average}")