python学习笔记 | 猜拳游戏

'''
@author: 人人都爱小雀斑
@time: 2020/3/6 18:52
@desc:
实验结果心得:
1、难点主要在判断谁输谁赢
2、挺好的
'''
import random
d={1:"石头",2:"剪刀",3:""}#定义规则
manC,compC=0,0#定义人和机的得分变量

def cal(manInput,compInput):#判断胜负
    global manC, compC
    if manInput==1:#玩家出石头
        if compInput==1:print("平局!")
        elif compInput==2:
            manC+=1
            print("这局你赢了!")
        elif compInput==3:
            compC+=1
            print("这局计算机赢了!")
    elif manInput==2:#玩家出剪刀
        if compInput==2:print("平局!")
        elif compInput==3:
            manC+=1
            print("这局你赢了!")
        elif compInput==1:
            compC+=1
            print("这局计算机赢了!")
    elif manInput==3:#玩家出布
        if compInput==3:print("平局!")
        elif compInput==1:
            manC+=1
            print("这局你赢了!")
        elif compInput==2:
            compC+=1
            print("这局计算机赢了!")

print("规则如下:
1、数字 1代表石头,2代表剪刀,3代表布
2、出手相同则不分胜负,不加分、不减分;二人不同,胜者加1分,负者减1分
")
name=input("请输入您的名字:")
while True:
    while True:
        manInput = int(input("
>>>>>>请输入1\2\3:"))  # 人出手了
        if manInput in [1,2,3]:break
    compInput=random.choice([1,2,3])#计算机出手了
    print("您出了:{}
计算机出了:{}".format(d[manInput],d[compInput]))
    cal(manInput,compInput)
    print("
此时的战况:
{}-{}分
{}-{}分".format(name,manC,"计算机",compC))
原文地址:https://www.cnblogs.com/billie52707/p/12496493.html