Loading

猜数字2

1-实例代码

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

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

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

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

	if not ret:
		# 没猜对情况下提示剩余次数
		if cnt==0:
			print("很遗憾,你猜了{}次都失败了。".format(total))
		else:
			print("还剩{}次机会".format(cnt))


2-运行截图


posted @ 2023-06-04 16:12  Devinwon  阅读(50)  评论(0)    收藏  举报