python基础(3)-----循环

if循环

name=["sdf","sadf","awdasd","ljy"]
for user in name:
    if user=="ljy":
        print(user.upper())#全部大写
    else:
        print(user.title())#首字母大写
name="ljy"
if name != "LL": #if不等于
    print("dont mattached")

user="ljy1"
if user not in name:     #检查特定值是否在列表中
    print(user+" not in name")

if-else循环

"""if-else简单应用实例"""
age=18
if age>=12:
    print("you can go")
else:
    print("you cant go")

"""实例if-elif-else"""
age=18
if age<12:
    print("you can go 1 ")
elif age<18:
    print("you can go 2 ")
else:
    print("you can go 3 ")

"""多个elif代码块"""
age=18
if age<12:
    print("you can go 1 ")
elif age<18:
    print("you can go 2 ")
elif age<23:
    print("you can go 6 ")
else:
    print("you can go 3 ")

while循环

age=1
while age<4:
    print(age)
    age +=1

 break和continue

while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环

for i in range (1,3):
    print
    print('i=',i)
    print('Hello,how')
    if i==2:
        break
    print('are you today?')

for i in range (1,3):
    print
    print('i=',i)
    print('Hello,how')
    if i==2:
        continue
    print('are you today?')

 

posted @ 2019-01-31 11:49  jayonlau  阅读(247)  评论(0编辑  收藏  举报