samedi 27 juin 2020

Variable scopes in Python3

here is a piece of a very very messy code. It doesn't matter as much, since I just did it for fun. Everything works as expected until the loop ends and I am supposed to get the number of times the variables "correct" and "incorrect" were increased. The output is 0, and I don't get why. Any ideas?

import random
correct = 0
incorrect = 0

x = 100
while x >= 1:
    x = x-1

    #door with number one will the the one with the price behind it
    a = 0
    b = 0
    c = 0
    first_door_chosen = ""
    #randomly choose which door is the one with the price
    random_door = random.randint(1,3)

    if random_door == 1:
        a = 1
        print("---------FIRST DOOR IS THE RIGHT ONE")
    if random_door == 2:
        b = 1
        print("---------SECOND DOOR IS THE RIGHT ONE")
    if random_door == 3:
        c = 1
        print("---------THIRD DOOR IS THE RIGHT ONE")
    ##################END#####################

    #randomly choose which door YOU want to choose.
    random_door_selection = random.randint(1,3)

    if random_door_selection == 1:
        print("You choose door 1.")
    if random_door_selection == 2:
        print("You choose door 2.")
    if random_door_selection == 3:
        print("You choose door 3.")
    ###################END#####################

    #Stop the game if the door you chose is the one with the prize.
    if a == 1 and random_door_selection == 1:
        print("First door was the right door")
        first_door_chosen == "a"
        
    elif b == 1 and random_door_selection == 2:
        print("Second door was the right door")
        first_door_chosen == "b"
        
    elif c == 1 and random_door_selection == 3:
        print("Third door was the right door")
        first_door_chosen == "c"
        
    else:

        
    ####################END#################### 

            
        print("Your door was wrong.")
        print("For the purposes of this experiment, we will automatically discard your chosen door.")
        print("You automatically change your door with the other one that is still closed.")

        if random_door_selection == 1:
            if random_door == 2:
                print("The door with the prize was the SECOND one.")
                if random_door_selection == 2:
                    print("You LOSE")
                    incorrect += 1
                if random_door_selection == 3:
                    print("You WIN")
                    correct += 1
            if random_door == 3:
                print("The door with the prize was the THIRD one.")
                if random_door_selection == 2:
                    print("You WIN")
                    correct += 1
                if random_door_selection == 3:
                    print("You LOSE")
                    incorrect += 1
                
        if random_door_selection == 2:
            if random_door == 1:
                print("The door with the prize was the FIRST one.")
                if random_door_selection == 1:
                    print("You LOSE")
                    incorrect += 1
                if random_door_selection == 3:
                    print("You WIN")
                    correct += 1
            if random_door == 3:
                print("The door with the prize was the THIRD one.")
                if random_door_selection == 1:
                    print("You WIN")
                    correct += 1
                if random_door_selection == 3:
                    print("You LOSE")
                    incorrect += 1

        if random_door_selection == 3:
            if random_door == 1:
                print("The door with the prize was the FIRST one.")
                if random_door_selection == 1:
                    print("You LOSE")
                    incorrect += 1
                if random_door_selection == 2:
                    print("You WIN")
                    correct += 1
            if random_door == 2:
                print("The door with the prize was the SECOND one.")
                if random_door_selection == 2:
                    print("You WIN")
                    correct += 1
                if random_door_selection == 1:
                    print("You LOSE")
                    incorrect += 1

print("")
print("")


print("from %s tries, %s were incorrect" %(x, incorrect,))
print("from %s tries, %s were correct" %(x, correct,))

Thank you very much for your time !




Aucun commentaire:

Enregistrer un commentaire