亲宝软件园·资讯

展开

Python石头剪刀布

小鹿焕焕 人气:0

 从控制台输入要出的拳 —— 剪刀(0)/石头(1)/布(2)

 电脑 **随机** 出拳 

 比较胜负

增加错误输入的检测模块

# -*- coding = utf-8 -*-
# @Time :  22:03
# @Author : huanhuan
# @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)
        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("结束游戏")
                break
            else:
                print("电脑胜利")
    except ValueError:
        print("请输入数字而不是字母")

加载全部内容

相关教程
猜你喜欢
用户评论