Python 猜数字游戏 - NOTEBOOK
	Python 猜数字游戏
	PythonPosted on 2023-05-25
	
摘要 : 猜数字游戏:生成一个 1~100 的随机整数,设置最大猜测次数5。
	
 
	# 猜数字游戏
import random
# 生成一个 1~100 的随机整数
number = random.randint(1, 100)
# 猜测次数
tries = 0
# 最大猜测次数
max_tries = 5
while tries < max_tries:
	tries += 1
	# 提示玩家输入一个整数
	guess = input(f'Guess {tries}/{max_tries}, enter an integer between 1 and 100: ')
	# 将玩家输入转换为整数,如果无法转换,则提示输入错误
	try:
		guess = int(guess)
	except ValueError:
		print('Invalid input, please enter an integer.')
		continue
	# 判断猜测是否正确
	if guess == number:
		print(f'Congratulations! You guessed the number in {tries} tries!')
		break
	elif guess < number:
		print('Your guess is too low.')
	else:
		print('Your guess is too high.')
else:
	print(f'Sorry, you ran out of tries. The number was {number}.')