实现让用户不断的猜年龄,但只给最多3次机会,超过次数猜不对就退出程序。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Sandu"

my_age = 27
count = 0

while count < 3:
guess_age = int(input("请输入您猜的年龄:"))
count += 1
if guess_age > my_age:
print("年龄猜大了")
if count == 3:
print("已经超过输入次数了")
elif guess_age < my_age:
print("年龄猜小了")
if count == 3:
print("已经超过输入次数了")
else:
print("恭喜你,猜对了!")
break

############改良版本#####################
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Sandu"

my_age = 27
count = 0
while count < 3:
guess_age = int(input("请输入您猜的年龄:"))
if guess_age > my_age:
print("年龄猜大了")
elif guess_age < my_age:
print("年龄猜小了")
else:
print("恭喜你,猜对了!")
break
count += 1
else:
print("已经超过输入次数了")



posted @ 2016-11-05 11:59  哈喽哈喽111111  阅读(559)  评论(0编辑  收藏  举报