从控制台输入要出的拳 —— 剪刀(0)/石头(1)/布(2)
电脑 **随机** 出拳
比较胜负
增加错误输入的检测模块
# -*- coding = utf-8 -*- # @Time : 22:03 # @Author : huanh编程客栈uan # @File : test.py # @Software : PyCharm import random def show(num): if num == 0: sign = "剪刀" elif num == 1: sign = "石头" else: sign = "布" return sign while True: try: com = random.randint(0, 2) phttp://www.cppcns.comeople = 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("结束游戏") break else: 编程客栈 print("电脑胜利") except编程客栈 ValueError: print("请输入数字而不是字母")
到此这篇关于用python实现石头剪刀布游戏的文章就介绍到这了,更多相关Python石头剪刀布内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论