python while 与 if 与for 与continue 与break介绍

1、简单的年龄guessGame:

age = 60
count = 0

while count <3:
guess_age = int(input("guess age:"))
if guess_age ==age:
print("yes,you got it.") #如果猜对 结束循环
break
elif guess_age < age:
print("smaller...")
else:
print("bigger !!!") #猜大 提醒猜大了或者小了
    count +=1
if count == 3: #限制猜3次
countine_confirm = input("Do you want to keep Guessing ?")
if countine_confirm != 'n': # 如果还要继续猜答 请输入 n 继续猜 否则 结束猜答
count = 0
else:
print("Wecome to paly GuessGame !!!")



2、for的使用
for i in range (0,10,2):  #2表示跳格 2个数字 打印
if i< 5:
print("loop",i)
else:
continue # 跳出本次循环 ;break 是结束本次循环
print("hehehe")

#打印结果:

loop 0
hehehe
loop 2
hehehe
loop 4
hehehe



posted on 2018-04-03 16:21  StarWord  阅读(210)  评论(0)    收藏  举报

导航