lundi 27 novembre 2017

Need python help for school asap please [random.shuffle()] NOT HOMEWORK [on hold]

BEFORE I START, I WANT TO SAY THIS IS NOT HOMEWORK, IT IS REVISION

Right now I am trying to do a piece of code for the Monty Hall problem

we were given some code that does not work and need to fix it, however, we were given specifics for some sections that need to be completed in a certain way.

These are some questions that I am stuck on:

1. Currently, the prize is always behind the same door. Fix this electronically by using random.shuffle().

I think this means change the code in the section around if door[choice-1] == "car":

But I am not sure.

2. Near the end, the selection statement refers to choice-1. Why does it use choice-1 instead of choice?

I think it is because is the user inputs "n" when asked

switch = input("There is a goat behind door " + goatDoor + \ " switch to door " + otherDoor + "? (y/n) ")

3. To observe the positive effect of switching doors, it would be useful if you could play multiple games. Change your program so 10 games are played before the program closes.

I have an idea that involves a while loop and a count variable, but I'm not sure

4. In a simulation, you should not need to manually enter your choice. Instead, we use randomness to pick for us. Change your program so instead of asking for an input, it automatically chooses one for you (at random).

5. Create a subroutine montyHall() using the code that you have written so far. The subroutine should take in two parameters - The first should indicate the number of games that should be played, and the second should indicate whether you should switch door for those games or not. Run it 1000 times with switching, and 1000 times without switching. In each test, you should output the number of times that you won the "car", and how many times you won a goat.

Please can I have some feedback on this? ASAP

here is the code that I was given before I edited it.


door = ["goat","goat","car"]

choice = input("Door 1, 2 or 3? ")
otherDoor = 0
goatDoor = 0

if choice == 1:
    if door[1] == "goat":
  otherDoor = 3 
  goatDoor = 2
    elif door[2] == "goat":
  otherDoor = 2
  goatDoor = 3  
elif choice == 2:
    if door[0] == "goat":
  otherDoor = 3
  goatDoor = 1
    elif door[2] == "goat":
  otherDoor = 1
  goatDoor = 3
elif choice == 3:
    if door[0] == "goat":
  otherDoor = 2
  goatDoor = 1
    elif door[1] == "goat":
  otherDoor = 1
  goatDoor = 2

switch = input("There is a goat behind door " + goatDoor + \
               " switch to door " + otherDoor + "? (y/n) ")

if switch == "y":
    choice = otherDoor

if door[choice-1] == "car":
    print("You won a car!")
else:
    print("You won a goat!")

input("Press enter to exit.")


I know there are a couple of errors in it, but that is what I am supposed to fix this is after, I have put #CHANGE/ above and #CHANGE\ below each change I have made.

door = ["goat","goat","car"]
#change/
choice = int(input("Door 1, 2 or 3? "))
#change\
otherDoor = 0
goatDoor = 0

if choice == 1:
    if door[1] == "goat":
        otherDoor = 3 
        goatDoor = 2
    elif door[2] == "goat":
        otherDoor = 2
        goatDoor = 3  
elif choice == 2:
    if door[0] == "goat":
        otherDoor = 3
        goatDoor = 1
    elif door[2] == "goat":
        otherDoor = 1
        goatDoor = 3
elif choice == 3:
 if door[0] == "goat":
     otherDoor = 2
     goatDoor = 1
 elif door[1] == "goat":
     otherDoor = 1
     goatDoor = 2
#change/
switch = input("There is a goat behind door " + str(goatDoor) +\
               " switch to door " + str(otherDoor) + "? (y/n) ")
#Change\
if switch == "y":
    choice = otherDoor

if door[choice-1] == "car":
    print("You won a car!")
else:
    print("You won a goat!")

input("Press enter to exit.")


I need help as soon as I can get it, I have a test tomorrow

This is one of the sites that I used to try and figure out the answer I need.




Aucun commentaire:

Enregistrer un commentaire