day-5while循环的运用

#引入random模块#
import random
#调用random模块中的随机整数函数#
secret = random.randint(1,10)
#输出旁白切割线+随机生成的数字,方便测试用(注意str(secret) 这么写是因为字符和整形不能相加,需要统一修改成字符串类型)#
print('-------------------------------------------' + str(secret))
#定义变量count用于while循环,限定循环次数#
count = 1
#开始while循环,限定循环3次,从1开始小于4一共三个数:1、2、3#
while count < 4:
#此处需注意如果要用temp.isdigit(),则不能直接定义 temp=int(input(......)),
# 所以先判断用户输入,然后再定义新变量等于int(temp)
temp = input("【请猜一猜我的幸运数字(只有三次机会呦~)现在是第" + str(count) + "次】:")
#增加个判定限制用户只能输入整形数字temp.isdigit() #
if temp.isdigit():
guess = int(temp)
if guess == secret:
print("【哎我去,你一定是我肚子里的蛔虫】 ")
break
else:
if guess > secret:
print("【数字大了,哈哈~~】")
else:
print("【数字小了,哈哈~~】")
count = count + 1
else:
print("【请输入1-10的整数数字】")
if count == 4:
print("【好笨啊,三次都猜不对!】")
print("【游戏结束,欢迎下次在玩~~么么哒~~】")


猜你喜欢

转载自www.cnblogs.com/pythonzhao/p/11620992.html