Python游戏《猜数字》

我的版本:

print('-------------我爱鱼c------------')
time = 3
import random
secret = random.randint(1,10)
temp = input ("你知道我心里想哪个数字吗?:")
while not temp.isdigit():
    temp = input("请输入一个整数:")
guess = int(temp)
while guess !=secret and time:
    time = time - 1
    temp = input ("哎呀,猜错了,请重新输入吧:")
    guess = int(temp)
    if guess == secret:
        print("哇草,你是我心里的蛔虫吗?")
        print("哼,就算猜中了也没有奖励,^_^")
    else:
        if guess > secret:
            print("哥,大了大了~~~")
        else:
            print("嘿,小了!小了!!")
print("游戏结束,不玩啦!")

老公的版本:

import random

#生成心中数字
def CreateNum():
    return random.randint(1, 10)

#将字符串转换为整形
def StrToInt(str):
    ret = float(str)
    return int(ret)

#负责输出正确的数据(str提示信息,errCount最大错误数)
def OuputVal(str, errCount):
    while True:
        if errCount == 0:
            return ""

        val = input(str)
        if not val.isdigit():
            print("请输入数字")
            errCount -= 1   #出现错误,则减1

        return val
    return

#猜一猜主函数
def CC():
    secret = CreateNum();

    time = 3
    while time != 0:
        strVal = OuputVal("你知道我心里想哪个数字吗?:", 3)
        #判断值是否为空,空则失败一次
        if len(strVal) == 0:
            time -= 1
            continue

        nVal = StrToInt(strVal)

        #猜一猜判断逻辑
        if secret == nVal:
            print("猜中了,奖励你个吻")
            #猜中了,退出游戏
            return
        elif secret < nVal:
            print("姐,有点大哦")
        else:
            print("小了小了")

        #更新次数
        time -= 1

    print("你都猜了3次了,结束游戏")

#调用函数
CC();
原文地址:https://www.cnblogs.com/zjn-20161215/p/8601598.html