import random
def main(): print("欢迎来到猜数字游戏!") print("我已经想好了一个1到100之间的数字。")
number = random.randint(1, 100)
attempts = 0
while True:
try:
guess = int(input("请输入你猜测的数字(1-100):"))
except ValueError:
print("无效输入,请输入一个1-100之间的整数。")
continue
attempts += 1
if guess == number:
print(f"恭喜你,猜对了!数字是 {number},你总共猜了 {attempts} 次。")
break
elif guess < number:
print("猜小了,请再试一次。")
else:
print("猜大了,请再试一次。")
if name == "main": main()