猜数字2

发布时间 2023-06-04 16:19:35作者: Devinwon

1-实例代码

'''---------------------------------
# @Date:   2023-06-04 15:10:24
# @Author: Devin
# @Last Modified: 2023-06-04 16:04:47
------------------------------------'''

'''
程序说明:
	电脑给出一个范围内的随机数,用户根据反馈尝试猜测,
	对猜测次数做了限制,超出猜测次数视为失败,没有猜对提示剩余的猜测次数。
VERSION:2
'''

import random
# 电脑给出1—10范围内的随机整数
number=random.randint(1,10)
cnt=int(input("限制猜测次数:"))
total=cnt
ret=False

while cnt>0 and not ret:
	# 用户尝试猜测数字
	guess=int(input("猜数字:"))
	cnt-=1
	if guess<number:
		print("猜小了...,请继续")
	elif guess>number:
		print("猜大了...,请继续")
	else:
		print("真棒,你猜了{}次就对了。".format(total-cnt))
		ret=True

	if not ret:
		print("还剩{}次机会".format(cnt))

2-运行截图