用Python实现石头剪刀布游戏

从控制台输入要出的拳 —— 剪刀(0)/石头(1)/布(2)
电脑 **随机** 出拳
比较胜负
增加错误输入的检测模块

# -*- coding = utf-8 -*-# @Time :22:03# @Author : huanhuan# @File : test.py# @Software : PyCharmimport random def show(num):if num == 0:sign = "剪刀"elif num == 1:sign = "石头"else:sign = "布"return sign while True:try:com = random.randint(0, 2)people = int(input("请输入(剪刀:0,石头:1,布:2):"))if people > 3 or people < 0:print("请输入正确的数字")else:print("玩家出%s,电脑出%s" % (show(people), show(com)))if (people == 0 and com == 2) \or (people == 1 and com == 0) \or (people == 2 and com == 1):print("玩家胜利")elif people == com:print("双方平局")elif people == 3:print("结束游戏")breakelse:print("电脑胜利")except ValueError:print("请输入数字而不是字母")

用Python实现石头剪刀布游戏
文章图片

【用Python实现石头剪刀布游戏】到此这篇关于用Python实现石头剪刀布游戏的文章就介绍到这了,更多相关Python石头剪刀布内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读