程序代码:
import random #导入随机工具包
def menu(): #制作菜单
print("\t\t来玩一局剪刀石头布吧")
print("\t\t[1]剪刀 [2]石头 [3]布")
list=["剪刀","石头","布"] #建立一个列表,方便后续结果输出
computer=random.randint(1,3) #生成随机数,随机生成电脑的选择
win=0
lose=0
round=0
while True: #建立循环,实现多次游戏的进行
menu()
answer=int(input("\t\t输入你要出的序号")) #玩家的选择
if answer==computer:
print(f"\t\t你和电脑都出了{list[answer-1]}")
round+=1
elif answer==1 and computer==3 or answer==2 and computer==1 or answer==3 and computer==2:
print(f"\t\t你出的{list[answer-1]},电脑出的{list[computer-1]},你赢了")
win+=1
round+=1
elif answer==1 and computer==2 or answer==2 and computer==3 or answer==3 and computer==1:
print(f"\t\t你出的{list[answer-1]},电脑出的{list[computer-1]},你输了")
lose+=1
round+=1
print("")
x=input("\t\t还想继续玩吗?(yes or no)")
if x!="yes": #判断是否继续
break
print(f"本次游戏总共有{round}把",f"你赢了{win}把,电脑赢了{lose}把") #输出游戏结果
if win>lose:
print("你赢了")
elif win==lose:
print("平局")
else:
print("你输了")
代码运行情况: