python笔记之循环控制

学习python的第一个例子,while循环中嵌套if-else语句,一个猜年龄的例子

#案例1,实现循环猜年龄
# my_age = 12
# while True:
# guess_age1 = input("guess age fo Kevin:")
# guess_age2 = int(guess_age1)
# if guess_age2 > my_age:
# print("you guess bigger")
# elif guess_age2 < my_age:
# print("you guess small")
# else:
# print("yes,my age is %s,you got it" %(my_age))
# break
#案例2,不用break语句,可以实现猜对退出脚本
# my_age = 12
# guess_age = 0
# while guess_age != my_age:
# guess_age = int(input("guess my age:"))
# if guess_age > my_age:
# print("you guess bigger")
# elif guess_age == my_age:
# pass
# else:
# print("you guess small")
# print("yes you got it my age is %d" %(my_age))

  

#案例3,实现可以猜5次,五次到了如果还没有猜到,则问是否要继续猜,如果继续,则还可以猜五次,如果不继续则直接退出
# my_age = 12
# flag = 1
# times = 0
# while flag <= 6:
# if flag == 6:
# option = input("do you want to continue?")
# if option == 'no':
# print("byebye")
# break
# else:
# flag = 1
# continue
# gusee_age = int(input("guess age of Kevin:"))
# flag += 1
# if gusee_age < my_age:
# times += 1
# print("[you guess %d times]" %(times))
# print("you guess small.")
# elif gusee_age > my_age:
# times += 1
# print("[you guess %d times]" % (times))
# print("you guess big.")
# else:
# times += 1
# print("[you guess %d times]" % (times))
# print("yes you got it,my age is %d" %(my_age))
# break

  

上一篇:第二阶段冲刺——four


下一篇:【转】Python基础语法