题目要求:
定一个数字(1-10,随机产生,通过3次判断来猜出数字)
- 数字随机产生,范围1-10
- 有三次机会猜测数字,通过3层嵌套判断实现
- 每次猜不中会提示大了或者小了
ps:补充随机函数
import random
num = random.randint(1, 10)
import randomnum = random.randint(1, 10)import randomnum = random.randint(1, 10)
guess_num = int(input("请输入您猜的数字:"))
if guess_num == num:print("恭喜,第1次就猜对了")
else:if guess_num > num:print("第1次猜的数字比num大")else:print("第1次猜的数字比num小")guess_num = int(input("请输入您猜的数字:"))if guess_num == num:print("恭喜,第2次猜对了")else:if guess_num > num:print("第2次猜的数字比num大")else:print("第2次猜的数字比num小")guess_num = int(input("请输入您猜的数字:"))if guess_num == num:print("恭喜,第3次猜对了")else:if guess_num > num:print("第3次猜的数字比num大")else:print("第3次猜的数字比num小")
运行截图: