zoukankan      html  css  js  c++  java
  • 基础运算符补充,流程控制之if判断/while循环

    常量

    常量即指不变的量.在python中没有一个专门 的语法代表常量,程序员约定俗成地用变量名全部被大写代表常量.

    AGE_OF_OLDBOY = 56

    基础运算符补充

    1.算术运算

    加减乘除+ - * /

    % 取模(返回除法的余数) 如 20%10=0

    ** 幂

    // 取整数(返回商的整数部分) 如 9//2=4

    2.赋值运算

    • 增量赋值

        
      age=18
      age+=1 #age=age+1

      age=18
      age /=3   #age=age/3

      age**2    #age =age**2
    • 交叉赋值

        
      x=1
      y=2

      # temp=x
      # x=y
      # y=temp
      # print (x,y)

      x,y=y,x
      print(x,y)
    • 链式赋值

        
      x=10
      y=x
      z=y
      x=y=z=10
    • 解压赋值

        
      l=[1,2,3,4,5]

      #将数据从l中解压出来(打印出l中的数据)
      a=l[0]
      b=l[1]
      c=l[2]
      d=l[3]
      e=l[4] #这种方法繁琐

      a,b,c,d,e=l #更加方便

      l=[1,2,3,4,5]
      a,b,*_=l # *_代表了列表中a,b之外的其他元素
      a,*_,b=l
      *_,a,b=l

    流程控制之if判断

    语法一:

    if 条件: 缩进代码块

      age_of_bk=30
    print('star....')
    inp_age=input('please input your age:')
    inp_age=int(inp_age)    #int将inp_age由字符串类型转变成整型,便于进行比较
    if inp_age==age_of_bk:
       print('猜对了')
    print('end....')

    语法二:if+else

    if 条件: 缩进代码块

    else : 缩进代码块

      
    age_of_girl=18
    height=170
    weight=90
    is_pretty=True
    if age_of_girl >=18 and age_of_girl < 22 and height >160 and is_pretty:
       print('表白')
    else:
       print('阿姨好')

    语法三:if+elif

    if 条件1 缩进代码块elif 条件2 缩进代码块elif 条件3 缩进代码块else: 缩进代码块

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

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

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

          其他情况:很差
    '''

    score=input('please input your score:')
    score=int(score)
    if score>=90:
       print('优秀')
    elif score>=80:        #不用'and<90',因为if语句是一条成立不会运行下一条
       print('良好')
    elif score>=70:
       print('普通')
    else:
       print('很差')

    语法四:if套if

    if 条件1: if 条件2: 缩进代码块 缩进代码块

      
    age_of_girl=18
    height=171
    weight=99
    is_pretty=True
    success=True
    if age_of_girl>=18 and age_of_girl<22 and height > 170 and is_pretty:
       if success:
           print('表白成功,在一起')
       else:
           print('不要爱情')
    else:
       print('阿姨好')

    流程控制之循环(while循环)

    while循环:条件循环

    如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件

    如果条件为假,那么循环体不执行,循环终止

    1.while 条件: 缩进代码块

      
    name_bk='kopa'
    pwd_bk='123'
    tag=True
    while tag:
       name_inp=input('please input your name:')
       pwd_inp=input('please your password:')
       if name_inp==name_bk and pwd_inp==pwd_bk:
           print('login successful')
           tag=False
       else :
           print('your name or password erorr')

    2.while+break: break代表结束本层循环

      
    name_bk='kopa'
    pwd_bk='123'

    while True:
       name_inp=input('please input your name:')
       pwd_inp=input('please your password:')
       if name_inp==name_bk and pwd_inp==pwd_bk:
           print('login successful')
           break
       else :
           print('your name or password erorr')

    3.while+continue: continue代表结束本次循环,直接进去下一次

      
    count=1
    while count<6:
       if count==3:
           count+=1
           continue
       print(count)
       count+=1 #如果print之后不加count+=1就会出现死循环
      #输错三次退出
    name_bk='kopa'
    pwd_bk='123'
    count=0
    while True:
       if count==3:
           print('输入次数过多')
           break
       inp_name=input('please input your name:')
       inp_pwd=input('please input your password:')
       if inp_name==name_bk and inp_pwd==pwd_bk:
           print('login success')
           break
       else:
           print('your name or pwd erorr')
           count+=1

    4.while+else

      count=0
    while True:
       if count == 10:
           break
       print(count)
       count+=1

    else:
       print("else的子代块只有在while循环没有被break打断的情况下才会执行")
      count=0
    while count <= 10:
       print(count)
       count+=1

    else:
       print("else的子代块只有在while循环没有被break打断的情况下才会执行")

    总结

      name_of_bk='kopa'
    pwd_of_bk='123'
    tag=True
    count=0

    while tag:
       if count == 3:
           print('输错的次数过多')
           break
       inp_name=input('your name>>:')
       inp_pwd=input('your password>>:')
       if inp_name==name_of_bk and inp_pwd == pwd_of_bk:
           print('login successful')
           while tag:
               print('''
              0 退出
              1 购物
              2 支付
              3 查看购物
              ''')
               cmd=input('>>>:')
               if cmd == '0':
                   tag = False
                   continue
               elif cmd == '1':
                   print('购物')
               elif cmd == '2':
                   print('支付')
               elif cmd == '3':
                   print('查看购物')
               else:
                   print('请输入0,1,2,3')
       else:
           print('your name or password erorr')
           count+=1
  • 相关阅读:
    C++ 沉思录——Chap6:句柄2
    C++ 沉思录——Chap5:代理类
    C++ 沉思录——Chap4:设计类的核查表
    Linux 网卡驱动相关——01
    FCoE的提出
    想成为嵌入式程序员应知道的0x10个基本问题
    C++ 沉思录——Chap6:句柄
    C++ 沉思录——Chap8:一个面向对象程序范例
    数据库调优积累系列(1):索引
    QTP使用问题集锦
  • 原文地址:https://www.cnblogs.com/kp1995/p/9991101.html
Copyright © 2011-2022 走看看