vendredi 18 juin 2021

Monty Hall Problem not working as intended

import random

total_winnings = 0
total_changed_wins = 0
total_losses = 0

def monthall(repeat):
    global total_winnings, total_losses, total_changed_wins
    for repeating in range(repeat):

        winning_choice = random.randint(1, 3)
        user_choice = random.randint(1, 3)

        if winning_choice == 1 and user_choice == 1:
            removed_choice = random.randint(2, 3)
        elif winning_choice == 2 and user_choice == 1:
            removed_choice = 3
        elif winning_choice == 3 and user_choice == 1:
            removed_choice = 2

        elif winning_choice == 1 and user_choice == 2:
            removed_choice = 3
        elif winning_choice == 2 and user_choice == 2:
            removed_choice = random.randrange(1, 4, 2)
        elif winning_choice == 3 and user_choice == 2:
            removed_choice = 1
        

        elif winning_choice == 1 and user_choice == 3:
            removed_choice = 2
        elif winning_choice == 2 and user_choice == 3:
            removed_choice = 1
        elif winning_choice == 3 and user_choice == 3:
            removed_choice = random.randint(1, 2)
        

        switch_not = random.randint(1, 2)

        if removed_choice == 1 and user_choice == 2 and switch_not == 1:
            if user_choice == winning_choice:
                total_winnings += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 1 and user_choice == 2 and switch_not == 2:
            user_choice = 3
            if user_choice == winning_choice:
                total_winnings += 1
                total_changed_wins += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 2 and user_choice == 1 and switch_not == 1:
            if user_choice == winning_choice:
                total_winnings += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 2 and user_choice == 1 and switch_not == 2:
            user_choice = 3
            if user_choice == winning_choice:
                total_winnings += 1
                total_changed_wins += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 3 and user_choice == 1 and switch_not == 1:
            if user_choice == winning_choice:
                total_winnings += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 3 and user_choice == 1 and switch_not == 2:
            user_choice = 2
            if user_choice == winning_choice:
                total_winnings += 1
                total_changed_wins += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 3 and user_choice == 2 and switch_not == 1:
            if user_choice == winning_choice:
                total_winnings += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 3 and user_choice == 2 and switch_not == 2:
            user_choice = 1
            if user_choice == winning_choice:
                total_winnings += 1
                total_changed_wins += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 2 and user_choice == 3 and switch_not == 1:
            if user_choice == winning_choice:
                total_winnings += 1
            elif user_choice != winning_choice:
                total_losses += 1

        elif removed_choice == 2 and user_choice == 3 and switch_not == 2:
            user_choice = 1
            if user_choice == winning_choice:
                total_winnings += 1
                total_changed_wins += 1
            elif user_choice != winning_choice:
                total_losses += 1 



monthall(10)

total_unchanged_wins = total_winnings - total_changed_wins
total_winnings = "{:,}".format(total_winnings)
total_losses = "{:,}".format(total_losses)
total_changed_wins = "{:,}".format(total_changed_wins)
total_unchanged_wins = "{:,}".format(total_unchanged_wins)


print(f"Total winnings: {total_winnings} ,\nChanged Doors Winnings: {total_changed_wins} ,\nUnchaged Doors winnings {total_unchanged_wins} ,\nTotal losses: {total_losses}")

Yes, I KNOW this is the worst code you have probably seen, but please, this is my first big project and I do not know how to iterate this, I tried searching it up on youtube and look at other peoples code, never understood what they meant (as your not supposed to, unprofessional and pointless) so I took the long route.

The only issue I am facing is that the total winnings + total losses do not add up to the number of times repeated (yes, I know, my fault for doing it this way). Is there any way that I could improve this code by watching a YT video that shows automation like this, and what is the solution to solve the issue?




Aucun commentaire:

Enregistrer un commentaire