import random

def guess_number():
    target = random.randint(1, 100)
    attempts = 10

    print("我已经想好了一个1到100之间的整数,你有10次机会猜到它。")

    while attempts > 0:
        try:
            guess = int(input("请输入你猜测的数字:"))
        except ValueError:
            print("输入错误,请输入一个整数。")
            continue

        if guess < target:
            print("猜小了!")
        elif guess > target:
            print("猜大了!")
        else:
            print("恭喜你,猜对了!")
            break

        attempts -= 1
        print(f"你还有{attempts}次机会。")

    if attempts == 0:
        print(f"很遗憾,你没有猜对。正确答案是{target}。")

if __name__ == "__main__":
    guess_number()

0 comments

No comments so far...