Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def monty_hall(door_selection=None, _choice=None):
  5. doors = ["goat" for _ in range(3)]
  6. car_index = random.randint(0, 2)
  7. doors[car_index] = "CAR"
  8.  
  9. door_number = door_selection
  10. if door_number is None:
  11. door_number = int(input("Vyberte dvere c. 1, 2 nebo 3\n")) - 1
  12.  
  13. l = [0, 1, 2]
  14. l.remove(door_number)
  15.  
  16. if door_number == car_index:
  17. opened_index = random.choice(l)
  18. if door_selection is None:
  19. print("Otevreny dvere c", str(opened_index + 1))
  20. else:
  21. l.remove(car_index)
  22. opened_index = l[0]
  23. if door_selection is None:
  24. print("Otevreny dvere c", str(opened_index + 1))
  25.  
  26. if door_selection is None:
  27. print("chcete zmenit volbu dveri?")
  28.  
  29. choice = _choice
  30. if choice is None:
  31. choice = input("Ano (a), Ne (n), Nahoda (r)\n")
  32.  
  33. if choice == "r":
  34. choice = random.choice(["a", "n"])
  35.  
  36. l = [0, 1, 2]
  37. l.remove(door_number)
  38. l.remove(opened_index)
  39. if choice == "n":
  40. if door_selection is None:
  41. print(doors[door_number])
  42. if doors[door_number] == "CAR":
  43. return True
  44. else:
  45. return False
  46. elif choice == "a":
  47. if door_selection is None:
  48. print(doors[l[0]])
  49. if doors[l[0]] == "CAR":
  50. return True
  51. else:
  52. return False
  53.  
  54.  
  55. count_yes = 0
  56. count_no = 0
  57. count_rand = 0
  58.  
  59. for i in range(10000):
  60. res = monty_hall(random.randint(0, 2), "a")
  61. if res:
  62. count_yes += 1
  63.  
  64. res = monty_hall(random.randint(0, 2), "n")
  65. if res:
  66. count_no += 1
  67.  
  68. res = monty_hall(random.randint(0, 2), "r")
  69. if res:
  70. count_rand += 1
  71.  
  72. print("Wins, when decision changed", (count_yes / 10000) * 100, "%")
  73. print("Wins, when decision stayed", (count_no / 10000) * 100, "%")
  74. print("Wins, when decision was random", (count_rand / 10000) * 100, "%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement