zoukankan      html  css  js  c++  java
  • day04

    一、if判断

    语法一:                            

               if 条件:        

                    代码1          

                 代码2         

                    代码3         

    示例:sex='male'

               height=180

               if sex=='male' and height>178:

                   print('符合')

               print('你好')

    语法二:

        if 条件:

                    代码1          

                  代码2         

                     代码3

                 else              (条件不成立时执行子代码块)

                    代码1              

                  代码2         

                     代码3

    示例:

               sex='male'

               height=180

               if sex=='male' and height>178:

                   print('符合')

               else:

                   print('你好') 

    语法三:(if嵌套)

        if 条件1:

                    if 条件2

                        代码1          

                      代码2         

                         代码3

    示例:

               sex='male'

               height=180

               age=20

               if sex=='male' and height>178:

                   print('符合')

                   if age<30 :

                       print('你好') 

                   else:

                        print('再见')

               else:

                   print('不符合')

    语法四:

        if 条件1:(只有在条件1不成立时执行条件2)

                    代码1          

                 代码2         

                    代码3

                elif 条件2:

                     代码1           

                   代码2         

                      代码3

                elif 条件3:

                      代码1           

                   代码2         

                      代码3

    示例:

               score=input('please input your score: ')

               score=int(score)

               if score>=90:

                   print('优秀')

               elif score>=80:

                   print('良好')

               elif score>=70:

                   print('普通')

               else :

                   print('很差')

                 

    二、while循环

     1、语法:

        while 条件:

                    代码1          

                  代码2         

                     代码3

      2、结束while循环的方式

    方式一、条件改为false

                  在条件改为false时不会立即结束循环,而是在下一次循环判断条件时才会生效

    示例:   tag=True

                  while tag:

                      name=input('plesse input your name:  ')

                      pwd=input('please input your password:  ')

                      if name=='egon' and pwd=='123':

                          print('login sucessful')

                          tag=False

                      else:

                          print('username or password error')

                      print('>>>')                      

    方式二:while+break

                 break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环

    示例:   while True:

                      name=input('plesse input your name:  ')

                      pwd=input('please input your password:  ')

                      if name=='egon' and pwd=='123':

                          print('login sucessful')

                          break

                      else:

                          print('username or password error')

                      print('>>>')              

    方式一种最后执行的结果会有>>>,方式二中则没有

    2、while+continue

         结束本次循环,直接进入下一次循环

    示例一:

    count=1

    while count<6:

        if count==4:

            count+=1

            countinue

        print(count)

        count+=1

    示例二:while True:

                      name=input('plesse input your name:  ')

                      pwd=input('please input your password:  ')

                      if name=='egon' and pwd=='123':

                          print('login sucessful')

                          break

                      else:

                          print('username or password error')

                          #continue # 此处加continue无用

    3、while的嵌套

     while 条件1: 

            while 条件2:

                代码1          

              代码2         

                 代码3

    示例一:while True:

                      name=input('plesse input your name:  ')

                      pwd=input('please input your password:  ')

                      if name=='egon' and pwd=='123':

                          print('login sucessful')

                          while True:

                               print(" " "

                               0 退出

                               1 取款

                               2 转账

                               3 查询

                               " " ")

                               choice=input('请输入你要执行的操作 :)

                                if chioce=='0':

                                    break

                                elif chioce=='1':

                                    print('取款。。。')

                                elif chioce=='2':

                                    print('转账。。。')

                                elif chioce=='3'

                                    print('查询。。。')

                                 else:

                                 print('输入指令错误,请重新输入')

                             break

                         else:

                             print('user name or password error')

    示例二:tag=True

                   while tag:

                        name=input('plesse input your name:  ')

                        pwd=input('please input your password:  ')

                        if name=='egon' and pwd=='123':

                            print('login sucessful')

                            while tag:

                                 print(" " "

                                 0 退出

                                 1 取款

                                 2 转账

                                 3 查询

                                 " " ")

                                 choice=input('请输入你要执行的操作 :)

                                 if chioce=='0':

                                     tag=False

                                 elif chioce=='1':

                                     print('取款。。。')

                                 elif chioce=='2':

                                     print('转账。。。')

                                 elif chioce=='3'

                                     print('查询。。。')

                                 else:

                                     print('输入指令错误,请重新输入')

                              break

                          else:

                              print('user name or password error')

    三、for循环

    1、 for循环的强大之处在于取值

    l=['a','b','c','d','e']

    for x in l:

        print(x)

    dic={'name':'egon','age':18,'gender':'male'}

    for x in dic:

        print(x)

    2、for+break

    nums=[11,22,33,44,55]

    for x in nums:

        if x==44:

            break

        print(x)

    3、for+continue

    nums=[11,22,33,44,55]

    for x in nums:

        if x=22 or x==44:

            continue

        print(x)

    4、for+else

    names=['egon','aaa','kevin','mac']

    for name in names:

        if name=='aaa':

             break

        print(name)

    else:

        print('>>>>>')

    5、for+range()

    range的用法

    range(1,5)  顾头不顾尾,若括号内只有一个值默认为末端值,默认步长为1

    range(1,5,2) 

    for i in range(5)

    print(i)

    6、for的嵌套(外层循环先运行一次,运行内层循环,内层循环都运行一遍,然后再运行外层循环)

    for i in range(3)

        for j in range (4)

            print(i,j)    

  • 相关阅读:
    tomcat最大连接数
    【转】性能测试总结---测试流程篇
    nginx访问量统计
    在Excel中计算过去某一天到今天一共多少天
    Linux服务器/etc/profile
    Linux服务器安装MySQL
    idea无法识别maven项目
    python3 实现RC4加解密
    Python实现128-ECB 解密
    BlowFish加解密原理与代码实现
  • 原文地址:https://www.cnblogs.com/yangyinyin/p/10574774.html
Copyright © 2011-2022 走看看