出一个简单囚徒困境的代码
时间: 2024-05-01 22:18:52 浏览: 14
以下是一个简单的囚徒困境的Python代码:
```python
player_A_choice = input("Player A, Cooperate (C) or Defect (D)? ")
player_B_choice = input("Player B, Cooperate (C) or Defect (D)? ")
if player_A_choice == "C" and player_B_choice == "C":
print("Both players cooperate. Each gets 3 points.")
elif player_A_choice == "C" and player_B_choice == "D":
print("Player A Cooperates and Player B Defects. Player A gets 0 points and Player B gets 5 points.")
elif player_A_choice == "D" and player_B_choice == "C":
print("Player A Defects and Player B Cooperates. Player A gets 5 points and Player B gets 0 points.")
else:
print("Both players Defect. Each gets 1 point.")
```
在这个代码中, `input()` 函数用于获取用户的选择,“C”表示合作,"D"表示背叛。根据两个用户的选择,代码会输出所得到的分数。在囚徒困境中,双方都选择背叛会导致最差的结果,而两个人都选择合作会导致最好的结果。