# 导入随机工具包
# 注意:在导入工具包的时候,应将语句放在文件的顶部,方便下方的代码在任何需要的时候可以使用
import random
# 设置一个i,作为第几次开始游戏的判断,显示正确的文字
i = 0
# 用循环进行游戏
while True:
if i == 0:
# 设置一个字符串变量start,作为游戏开始的判断
start = input("是否开始游戏(是y/否n):")
else:
start = input("是否再来一局(是y/否n):")
# 输入的若非y,Y,n,N,则重新输入,直至正确
if start != "y" and start != "Y" and start != "n" and start != "N":
print("输入错误,请重新输入")
start = input("是否开始游戏(是y/否n):")
# 若输入的是n,N,则游戏结束,跳出整个游戏循环
elif start == "n" or start == "N":
print("游戏结束")
break
# 若输入的是y,Y,则游戏开始
elif start == "y" or start == "Y":
if i == 0:
print("游戏开始")
else:
print("游戏继续")
while True:
# 从控制台输入要出的拳,1表示石头,2表示剪刀,3表示布
player = input("请出拳(1表示石头,2表示剪刀,3表示布):")
# 将输入的数字以汉字的形式输出,更为直观
if player == "1" or player == "2" or player == "3":
if player == "1":
player_str = "石头"
elif player == "2":
player_str = "剪刀"
elif player == "3":
player_str = "布"
break
else:
print("输入错误,请重新输入")
# 电脑随机出拳
computer = random.randint(1, 3)
# 将随机的数字以汉字的形式输出,更为直观
if computer == 1:
computer_str = "石头"
elif computer == 2:
computer_str = "剪刀"
elif computer == 3:
computer_str = "布"
computer = str(computer)
print("玩家选择的是%s - 电脑选择的是%s" % (player_str, computer_str))
"""
判断胜负
拳头1 胜 剪刀2;剪刀2 胜 布3;布3 胜 石头1
"""
# 玩家获胜的情况
if ((player == "1" and computer == "2") or
(player == "2" and computer == "3") or
(player == "3" and computer == "1")):
print("玩家获胜")
# 平局
elif player == computer:
print("平局")
# 电脑获胜
else:
print("电脑获胜")
# 正确显示再来一局的文字
i = 1
|
|