python两个人联机猜拳的代码
时间: 2023-03-28 07:01:12 浏览: 90
以下是 Python 两个人联机猜拳的代码:
import random
def play_game(player1, player2):
"""
玩家1和玩家2进行猜拳游戏
"""
# 定义石头、剪刀、布的代号
ROCK =
SCISSORS = 1
PAPER = 2
# 定义猜拳结果的字典
result_dict = {
(ROCK, ROCK): "平局",
(ROCK, SCISSORS): "玩家1胜利",
(ROCK, PAPER): "玩家2胜利",
(SCISSORS, ROCK): "玩家2胜利",
(SCISSORS, SCISSORS): "平局",
(SCISSORS, PAPER): "玩家1胜利",
(PAPER, ROCK): "玩家1胜利",
(PAPER, SCISSORS): "玩家2胜利",
(PAPER, PAPER): "平局"
}
# 玩家1和玩家2进行猜拳
player1_choice = random.randint(, 2)
player2_choice = random.randint(, 2)
# 输出猜拳结果
print("玩家1选择了:", player1_choice)
print("玩家2选择了:", player2_choice)
print("猜拳结果:", result_dict[(player1_choice, player2_choice)])
# 测试代码
play_game("玩家1", "玩家2")
阅读全文