目标

掌握导包与包的应用,循环语句while,加深条件语句if... elif... else

实现

玩家与电脑玩猜拳游戏,记录输赢与总结

代码

# 导入随机包,以便电脑随机出拳调用
import random

# 声明3个变量,又来储存猜拳结果
win = 0
same = 0
lose = 0

# 用一个死循环,让猜拳游戏重复进行
while True:
    # 用15个等于号分隔每一次游戏内容
    print("====="*3)
    # 玩家出拳
    Player = int(input("请输入序号-1石头 2剪刀 3布:"))
    # 电脑随机出拳
    Computer = random.randint(1, 3)
    # 显示双方出的是啥
    print("你出的是%d,电脑出的是%d" % (Player, Computer))
    '''
    1.判断是否是我赢,显示战况
    2.判断是否是平手,显示战况
    3.不是我赢或平手就是我输,显示战况
    '''
    if ((Player == 1 and Computer == 2)
            or (Player == 2 and Computer == 3)
            or (Player == 3 and Computer == 1)):
        print("I win")
        win += 1
        print("战况: 赢%d局, 输%d局, 平%d局" % (win, lose, same))
    elif Player == Computer:
        print("same")
        same += 1
        print("战况: 赢%d局, 输%d局, 平%d局" % (win, lose, same))
    else:
        print("I lose")
        lose += 1
        print("战况: 赢%d局, 输%d局, 平%d局" % (win, lose, same))