Guest User

Prisoner Dilemma Code

a guest
Sep 26th, 2025
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. from random import randint
  2.  
  3. numberSim = int(input("Pick a number of simulations to run, between 10 and 500: "))
  4. while numberSim <= 10 or numberSim >= 500:
  5. numberSim = int(input("Please enter a number between 10 and 500: "))
  6.  
  7. if numberSim >= 10 or numberSim <= 500:
  8. print("Excellent, now, pick a strategy:")
  9. print("1: Random")
  10. print("2: Always be silent")
  11. print("3: Always cooperate")
  12. partnerAstrategy = int(input("Enter your choice: "))
  13. while partnerAstrategy > 3 or partnerAstrategy < 1:
  14. partnerAstrategy = int(input("Enter a number from 1 to 3: "))
  15.  
  16. if partnerAstrategy == 1:
  17. partnerAstrategy = randint(1, 2)
  18.  
  19. if partnerAstrategy == 2:
  20. partnerAstrategy = 2
  21.  
  22. if partnerAstrategy == 3:
  23. partnerAstrategy = 1
  24.  
  25. if partnerAstrategy == 1 or partnerAstrategy == 2:
  26. print("Excellent, now, pick a strategy for your partner:")
  27. print("1: Random")
  28. print("2: Always be silent")
  29. print("3: Always cooperate")
  30. partnerBstrategy = int(input("Enter your choice: "))
  31. while partnerBstrategy > 3 or partnerBstrategy < 1:
  32. partnerBstrategy = int(input("Enter a number from 1 to 3: "))
  33.  
  34. if partnerBstrategy == 1:
  35. partnerBstrategy = randint(1, 2)
  36.  
  37. if partnerBstrategy == 2:
  38. partnerBstrategy = 2
  39.  
  40. if partnerBstrategy == 3:
  41. partnerBstrategy = 1
  42.  
  43. if partnerBstrategy <= 3 or partnerBstrategy >= 1:
  44. partnerAchoice = partnerAstrategy
  45. partnerBchoice = partnerBstrategy
  46.  
  47. partnerAtime = 0
  48. partnerBtime = 0
  49.  
  50. for i in range(numberSim):
  51. if partnerBchoice == 1 and partnerAchoice == 1:
  52. partnerAtime += 4
  53. partnerBtime += 4
  54. if partnerBchoice == 2 and partnerAchoice == 2:
  55. partnerAtime += 2
  56. partnerBtime += 2
  57. if partnerBchoice == 1 and partnerAchoice == 2:
  58. partnerAtime += 0
  59. partnerBtime += 6
  60. if partnerAchoice == 1 and partnerBchoice == 2:
  61. partnerAtime += 6
  62. partnerBtime += 0
  63.  
  64. valueA = float(partnerAtime / numberSim)
  65. valueB = float(partnerBtime / numberSim)
  66. print("You got an average of", valueA, "years")
  67. print("The computer got an average of", valueB, "years")
Advertisement
Add Comment
Please, Sign In to add comment