记录我的成长吧~

流程控制

If判断:

一、语法:

  1. if 条件:

    代码1

    代码2

代码3

例:

cls='human'

sex='female'

age=18

if cls == 'human' and sex == 'female' and age > 16 and age < 22:

     print('开始表白')

 print('end....')

 

  1. If + else语法

if 条件:

     代码1

     代码2

     代码3

     ...

 else:

     代码1

     代码2

     代码3

     ...

 

 例:

cls='human'

sex='female'

age=38

if cls == 'human' and sex == 'female' and age > 16 and age < 22:

     print('开始表白')

else:

     print('阿姨好')

 

print('end....')

  1. if + elif + else 语法

if 条件1:

    代码1

    代码2

    代码3

     ...

 elif 条件2:

    代码1

     代码2

    代码3

    ...

elif 条件3:

    代码1

     代码2

     代码3

    ...

............

else:

    代码1

   代码2

    代码3

    ...

例:

如果:成绩>=90,那么:优秀

 

如果成绩>=80<90,那么:良好

 

如果成绩>=70<80,那么:普通

 

其他情况:很差

score=input('your score: ')

score=int(score)

if score >= 90:

     print('优秀')

elif score >= 80:

     print('良好')

elif score >= 70:

     print('普通')

else:

     print('很差')

例:用户登录判断:

 user_from_db='kevin'

 pwd_from_db='123'

 

 user_from_inp=input('username>>>: ')

 pwd_from_inp=input('password>>>: ')

 

 if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:

     print('login successfull')

 else:

     prnt('user or password error')

If的嵌套

cls='human'

sex='female'

age=18

is_success=False

 

if cls == 'human' and sex == 'female' and age > 16 and age < 22:

    print('开始表白...')

    if is_success:

        print('在一起')

    else:

        print('我逗你玩呢....')

else:

    print('阿姨好')

 

print('end....')

 

 

二、while循环

  1. while 条件:

     code1

     code2

     code3

     ....

例:

 user_db='kevin'

 pwd_db='123'

 while True:

     inp_user=input('username>>: ')

     inp_pwd=input('password>>: ')

     if inp_user == user_db and inp_pwd == pwd_db:

         print('login successfull')

     else:

         print('user or password error')

  1. while+break

break的意思是终止掉当前层的循环,.执行其他代码

例:

user_db='egon'

pwd_db='123'

 

while True:

    inp_user=input('username>>: ')

    inp_pwd=input('password>>: ')

    if inp_user == user_db and inp_pwd == pwd_db:

        print('login successfull')

        break

    else:

        print('user or password error')

 

 

print('其他代码')

 

  1. while+continue

continue的意思是终止掉本次循环,.直接进入下一次循环

continue一定不要加到循环体最后一步执行的代码

例:

n=1

 while n <= 10:

     if n == 8:

         n += 1

         continue

     print(n)

 

4.while True

if 条件1:

         code1

         code2

         code3

         continue #无意义

     elif 条件1:

         code1

         continue #有意义

         code2

         code3

     elif 条件1:

         code1

         code2

         code3

         continue #无意义

     ....

     else:

         code1

         code2

         code3

         continue #无意义

4.while循环嵌套

user_db='kevin'

pwd_db='kevin'

 

while True:

    inp_user=input('username>>: ')

    inp_pwd=input('password>>: ')

    if inp_user == user_db and inp_pwd == pwd_db:

        print('login successfull')

        while True:

            cmd=input('请输入你要执行的命令: ')

            if cmd == 'q':

                break

            print('%s 功能执行...' %cmd)

        break

    else:

        print('user or password error')

 

 

print('end....')

 

 

4.while+(变量)

user_db='kevin'

pwd_db='123'

 

tag=True

while tag:

    inp_user=input('username>>: ')

    inp_pwd=input('password>>: ')

    if inp_user == user_db and inp_pwd == pwd_db:

        print('login successfull')

        while tag:

            cmd=input('请输入你要执行的命令: ')

            if cmd == 'q':

                tag=False

            else:

                print('%s 功能执行...' %cmd)

 

    else:

        print('user or password error')

 

 

print('end....')

 

5.while+else

n=1

while n < 5:

    print(n)

    n+=1

else:

print('在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码')

 

For循环

迭代式循环:for,语法如下

  for i in range(10):

    缩进的代码块

2 breakcontinue(同上)

循环嵌套结尾会放2个案例

 

names=['kevin','Xiedian','Chenyoude','Zhuchunyu']

n=0

 while n < len(names):

     print(names[n])

     n+=1

 

 

 names=['kevin','Xiedian','Chenyoude','Zhuchunyu']

 info={'name':'egon','age':18,'sex':'male'}

 

 for k in info:'

     print(k,info[k])

 

 for item in names:

     print(item)

 

 

 for i in range(1,10):

     print(i)

 

 for i in range(10): #默认的起始位置是0

     print(i)

 

 for i in range(1,10,2): #1 3  5  7  9

     print(i)

 

 names=['kevin','Xiedian','Chenyoude','Zhuchunyu']

 for i in range(len(names)):

     print(i,names[i])

 

 

4.for+break

 names=['kevin','Xiedian','Chenyoude','Zhuchunyu']

 for n in names:

     if n == 'kevin':

         break

     print(n)

 

for+continue

names=['kevin','Xiedian','Chenyoude','Zhuchunyu']

 for n in names:

     if n == 'egon':

         continue

     print(n)

 

 

5.for+else

names=['kevin','Xiedian','Chenyoude','Zhuchunyu']

for n in names:

     if n == 'egon':

         break

    print(n)

else:

print('=====>')

For嵌套

for i in range(5):

     print('========>第一层: %s<=========' %i)

     for j in range(3):

        print('          第二层: %s' %j)

 

循环嵌套案例:

打印九九乘法:

for i in range(1,10):

    for j in range(1,i+1):

        print('%s*%s=%s' %(i,j,i*j),end=' ')

    print()

 

打印金字塔:

#分析

'''

 

             #max_level=5

    *        #current_level=1,空格数=4*号数=1

   ***       #current_level=2,空格数=3,*号数=3

  *****      #current_level=3,空格数=2,*号数=5

 *******     #current_level=4,空格数=1,*号数=7

*********    #current_level=5,空格数=0,*号数=9

 

#数学表达式

空格数=max_level-current_level

*号数=2*current_level-1

 

'''

 

#实现

max_level=5

for current_level in range(1,max_level+1):

    for i in range(max_level-current_level):

        print(' ',end='') #在一行中连续打印多个空格

    for j in range(2*current_level-1):

        print('*',end='') #在一行中连续打印多个空格

    print()

posted on 2018-05-29 21:13  Kevin_WangSiWen  阅读(154)  评论(0编辑  收藏  举报

导航