import random
def guess_number():
target_number = random.randint(1, 100)
attempts = 0
while True:
guess = int(input("请输入一个1到100之间的整数:"))
attempts += 1
if guess < target_number:
print("猜小了,请再试一次!")
elif guess > target_number:
print("猜大了,请再试一次!")
else:
print("恭喜你,猜对了!")
break
print("你一共猜了", attempts, "次。")
# 调用函数开始游戏
guess_number()
标签:guess,数字,再试一次,python,number,attempts,小游戏,print,target From: https://blog.51cto.com/u_16058927/7039989