Question

I need help fixing my python3 code. I am trying to get my Monty hall game working correctly. Below is my code. When I run the code it repeats the first question and doesn't work until the 3rd attempt or sometimes more than that. How do I fix this? I also need to be able to make it run 5 times in a row.(like ask the user at least 5 times, so it is re-playable. I added a image of the output to show you.

import random

win = 0
lose = 0
playing = True

while playing:

    door = [1,2,3]
    x = 0
    a = random.choice(door) # Computer chooses the winning door
    b = int(input("Please choose a door (1-3): ")) # User guesses winning door -- .strip() is pointless
    if a == b:
        # If answer = guess, pick one of the remaining doors to open
        door.remove(a)
        x = random.choice(door)
        c = x
        print("You chose door {}".format(b))
        print("I choose door {}".format(c))

        # Tells user which 2 doors are now left:
        door = [1,2,3]
        door.remove(x)
        door.remove(a)
        d = door[0]
        print("You're left with doors {} and {}".format(b,d))
        choice = input("Will you SWITCH to door {}? y/n: ".format(d))

        # Ask user if they want to switch. y = Lose / n = win
        if choice == "y":
            print("Bad luck. You lost. You should have stuck as you already had the right answer.")
            lose += 1
        else:
            print("Yay! You made the right choice!")

Please choose a door (1-3): 2 Please choose a door (1-3): 3 Please choose a door (1-3): 3 You chose door 3 I choose door 2 Yo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

it's repeating because you programmed it that the program flow will continue only when you guessed the correct door in the first step itself( if a==b )

also there are some logical fallacies..the code with comments

code:

import random

win = 0

lose = 0

playing = True

ct=0

while playing:

if ct>=5:

break

else:

ct+=1

door = [1,2,3]

x = 0

a = random.choice(door) # Computer chooses the winning door

b = int(input("Please choose a door (1-3): ")) # User guesses winning door -- .strip() is pointless

if a==b:

door.remove(a) #if same remove that one

else: #not same remove both

door.remove(a)

door.remove(b)

c = random.choice(door) #the remaining door

print("You chose door {}".format(b))

print("I choose door {}".format(c))

# Tells user which 2 doors are now left:

door = [1,2,3]

door.remove(c)

door.remove(b)

d = door[0]

print("You're left with doors {} and {}".format(b,d))

choice = input("Will you SWITCH to door {}? y/n: ".format(d))

# Ask user if they want to switch. y = Lose / n = win

if choice == "y":

b=d

elif choice=="n":

pass

else: #not valid input

print('not valid input.. exiting')

break

if b!=a: #if user choice not same to picked by computer

print("Bad luck. You lost. You should have stuck as you already had the right answer.")

lose += 1

else:

print("Yay! You made the right choice!")

win+=1

print() #newline

indentation:

note: as you said to run the code for 5 turns i placed the condition for ct == 5, change the condition if you want to change it

output:

Add a comment
Know the answer?
Add Answer to:
I need help fixing my python3 code. I am trying to get my Monty hall game...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Game: Suppose you're on a game show, and you're given the choice of 3 doors....

    The Game: Suppose you're on a game show, and you're given the choice of 3 doors. Behind one door is a car, behind the others, goats. You start by choosing a door, say number 1, which remains closed for now. The game show host, who knows what's behind the doors, opens another door, say number 3, which reveals a goat. He says to you, "You've already chosen door number 1, now that I've shown you a goat behind door number...

  • Monty Hall Problem - Suppose you are going to be on a game show, and you...

    Monty Hall Problem - Suppose you are going to be on a game show, and you will be given the choice of three doors: Behind one door is a car; behind the other two doors are goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your...

  • Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and...

    Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and you are given the choice of three doors. Behind one door is a car, behind the others, goats. You pick a door, say #1, and the host, who knows what is behind the doors, opens another door, say #3, which has a goat. Here we assume that the host cannot open the door to expose the car and when he can open either of...

  • agree or disagree Starting off, this was extremely confusing and difficult to understand everything. Playing the...

    agree or disagree Starting off, this was extremely confusing and difficult to understand everything. Playing the game, I originally thought of once one door was out of the way- I now have a 50/50 chance of winning the car. After the experiments of pick and switch, I found that my previous thought was incorrect! There are still three doors in this equation. Having one of the three revealed is an advantage now. I have found my percentage to be extremely...

  • 1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question...

    1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question appeared in the "Ask Marilyn" column in Parade, a Sunday supplement to many newspapers across the United States: Suppose you're on a game show, and you're given the choice of three doors; behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3,...

  • 5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates...

    5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates that one of them has a car behind it, while the other two have goats. You win a car if you end up choosing a door with a car behind it. The game is conducted as follows: • You pick an initial door out of the three available. • The game show hosts then opens a door (out of the remaining two doors) with...

  • JAVA Project Please have the code written in JAVA This is a game I am trying...

    JAVA Project Please have the code written in JAVA This is a game I am trying to develop. Please let me know if its possible and what the base code is. Prompt User “Hello! Welcome to the game.” Prompt User “To begin the game, please enter a letter” Random word generator, will set the String to a new word After the user inputs a letter, the computer will enter a loop Once in the loop the computer will test each...

  • Please help me write these in R script / Code 1, Suppose you're on a game...

    Please help me write these in R script / Code 1, Suppose you're on a game show, and you're given the choice of three doors. Behind one door is a car; behind the others, goats. You pick a door, say #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat. He then says to you, "Do you want to pick door #2?" What is the probability of winning the car if...

  • I am trying to program a dice game in Visual Studios c++ but am having trouble....

    I am trying to program a dice game in Visual Studios c++ but am having trouble. The program allows the user to choose the number of rolls, not exceeding 100,000. I am trying to print out the number of times each number(1-6) was rolled, but it's not working. What am I doing wrong? The link below is a copy of my code. https://docs.google.com/document/d/1baKfnof4pjdQ4905hwXluFtbcA1Fs7QESMQKy45xdpE/edit

  • Can someone please rewrite this code using a while loop? Please write it in python. #The...

    Can someone please rewrite this code using a while loop? Please write it in python. #The winning number my_number = 12 #variable for the input name user_n = input("Hello! What is your name?") #Ask the user print("Well",user_n,": I am thinking of a number between 1 and 20." + "Take a guess.") user_n = float(input("Take a guess.")) #if statements if user_n == my_number: print("Good job" ,user_n, "You guessed my number!") if user_n > my_number: print("Your guess is high. You lose.") if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT