Python基础实现与电脑对战的剪刀石头布小游戏,练习if while输入和输出

通过剪刀石头布小游戏,练习python的if while  print input

# 实现剪刀石头布小游戏
import  random # import 引入其他模块
'''
多行注释
'''

# 程序的入口
if __name__ == '__main__':
    # 1、定义菜单
    print("======欢迎中软游戏========")
    print("1、开始游戏")
    print("2、退出游戏")
    cmdLine = input("请输入选项:")
    while True:
        if cmdLine == "1":
            print("游戏开始,游戏提示:1、剪刀,2、石头,3、布")
            # 2、电脑出拳
            computer = str(random.randint(1, 3))
            # 3、人出拳
            player = input("请出拳:")

            # 4、判断输赢
            if computer == player:
                print("平局")
                pass
            elif computer == "1" and player == "2": # {} == 缩进 else if elif  &&  and or not
                print("你赢了,电脑出的剪刀")
                pass
            elif computer == "1" and player == "3":
                print("你输了,电脑出的剪刀")
                pass
            elif computer == "2" and player == "1":
                print("你输了,电脑出的石头")
                pass
            elif computer == "2" and player == "3":
                print("你赢了,电脑出的石头")
                pass
            elif computer == "3" and player == "1":
                print("你赢了,电脑出的布")
                pass
            elif computer == "3" and player == "2":
                print("你输了,电脑出的布")
                pass
            pass

        cmdLine = input("继续游戏吗?y/n:")
        if cmdLine != 'y':
            break
        else:
            cmdLine = "1"
        pass
    pass
发布了34 篇原创文章 · 获赞 54 · 访问量 5010

猜你喜欢

转载自blog.csdn.net/nosprings/article/details/103753981