编写代码实现剪刀、石头、布的猜举消戏,玩家轴入1、2、3表示剪刀、石头、布,
时间: 2023-07-03 10:29:37 浏览: 91
好的,以下是 Python 3 的代码实现:
```
import random
def get_computer_choice():
return random.randint(1, 3)
def get_player_choice():
while True:
player_choice = input("请出拳:1.剪刀 2.石头 3.布\n")
if player_choice in ['1', '2', '3']:
return int(player_choice)
else:
print("请出正确的拳!")
def get_result(computer_choice, player_choice):
if computer_choice == player_choice:
return "平局"
elif (computer_choice == 1 and player_choice == 3) or (computer_choice == 2 and player_choice == 1) or (computer_choice == 3 and player_choice == 2):
return "你输了"
else:
return "你赢了"
def play():
while True:
computer_choice = get_computer_choice()
player_choice = get_player_choice()
print("电脑出拳:%d" % computer_choice)
result = get_result(computer_choice, player_choice)
print(result)
if result != "平局":
break
play()
```
通过上述代码,我们可以实现了一个简单的剪刀石头布游戏。你可以通过输入数字1、2、3来进行游戏,电脑会随机出拳,最终展示出胜负结果。
阅读全文