猜100以内数字游戏

猜100以内数字游戏,程序内先设定被猜的数值。
用户给出的值偏小则给出提示“太小”;
用户给出的值偏大,给出提示“太大”,
最多只能猜5次。
猜中了给出提示“恭喜你!”;
如果5次都没猜正确,给出“结束,是否继续”。
如果继续,再给5次机会,如果不继续,则结束。


import random


num = random.randint(0,100)
print("随机出来的数字为:", num)
count = 0
while count < 5:
    try:
        a = int(input("请输入一个整数:"))
        iscorrect = False
        if a == num:
            iscorrect = True
            print("恭喜你!")
            count = 5
        elif a > num:
            count += 1
            print("太大")
        else:
            count += 1
            print("太小")

        if not iscorrect and count == 5:
            str = input("结束,是否继续?继续输入y:")
            if str == "y":
                count = 0

    except ValueError:
        print('请输入整数')
    

太多人写了,我也有篇文章可以参考
通过游戏编程学Python(1)— 猜数字_请叫我问哥的博客-CSDN博客_猜数字游戏python代码

望采纳,谢谢!

import random
随机数 = random.randint(1, 100)
def guess():
    次数 = 0

    print('猜猜看我想的数字是多少?提示:1到100之间的整数')

    while 次数 < 6:
        print('猜猜看')    # 注意前面有4个空格,同一列的代码都处于while代码块
        数字 = input()
        数字 = int(数字)

        次数 = 次数 + 1

        if 数字 < 随机数:
            print('太小')    # 注意前面有8个空格,同一列的代码都处于if代码块

        if 数字 > 随机数:
            print('太大')

        if 数字 == 随机数:
            break
    if 数字 == 随机数:
        print('恭喜你')

guess()
iscontinue = input("结束,是否继续?(1:继续,0:退出)")
if iscontinue == '1':
    guess()
else:
    随机数 = str(随机数)
    print('很遗憾,我想的数字是' + 随机数)
    exit()