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

    一:基本运算符补充

    1.算数运算符

    2.赋值运算符

      2.1增量运算;

      age=18

      age+=1

      print(age)

      2.2交叉赋值

      x=10

      y=20

      x,y=y,x

      print(x,y)

      2.3链式赋值

      x=y=z=10

      print(x,y,z)

      2.4解压赋值

        l=[1.2,2.2,3.3,4.4]

      a=l[0]

      b=l[1]

      c=l[2]

      d=l[3]

      print(a,b,c,d)

      

      a,b,c,d=l

      print(a,b,c,d)

      a,b,*_=l

      print(a,b)

      下划线通常用于不需要的取值,用于占位,多个值可以用*_来表示

    二:流程控制之if判断

    语法1:

    if条件:

        代码1

        代码2

        ...

    age_of_bk=30
    print('start.....')
    inp_age=input('>>>: ') #inp_age='18'
    inp_age=int(inp_age)
    if inp_age == age_of_bk:
        print('猜对了')
    
    print('end.....')
    View Code

    语法2:

    if条件:

        代码1

        代码2

        ...

    else:

        代码1

        代码2

        ...

    age=38
    gender='male'
    is_beautiful=True
    
    if age >= 18 and age <= 25 and gender == 'female' and is_beautiful:
        print('开始表白。。。。')
    
    else:
        print('阿姨好')
    View Code

    语法3:

    if条件1:

        代码1

        代码2

        代码3

    elif条件2:

        代码1

        代码2

    elif条件3:

        代码1

        代码2

    else:

        代码1

        代码2

        ...

    '''
    如果:
            成绩>=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('很差')
    View Code

    语法4:

    if条件1:

        if条件2:

            代码1

            代码2

        代码1

        代码2

    age=18
    gender='female'
    is_beautiful=True
    is_successful=True
    
    if age >= 18 and age <= 25 and gender == 'female' and is_beautiful:
        print('开始表白。。。。')
        if is_successful:
            print('在一起')
        else:
            print('我逗你玩呢。。。')
    else:
        print('阿姨好')
    View Code

    三:流程控制之while循环

    1.while循环:条件循环

    while 条件:

      代码1

      代码2

    name='Grace'
    pwd='123'
    tag=True
    while tag:
        input_name=input('your name:')
        input_pwd=input('your password:')
        if input_name == name and input_pwd == pwd:
            print('login successful')
            tag=False
        else:
            print('username or password error')
    View Code

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

    name='Grace'
    pwd='123'
    while True:
        input_name=input('your name:')
        input_pwd=input('your password:')
        if input_name == name and input_pwd == pwd:
            print('login successful')
            break
        else:
            print('username or password error')
    View Code

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

    输错三次退出:

    name='Grace'
    pwd='123'
    count=0
    while count<3:
        input_name=input('your name:')
        input_pwd=input('your password:')
        if input_name == name and input_pwd ==pwd:
            print('login successful')
            break
        else:
            print('username or password error')
            count+=1
    View Code

    4.while+else

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

  • 相关阅读:
    sdnu 1513 字符串翻转
    hdu-1559 最大子矩阵(二维树状数组模板题)
    hdu-1556 树状数组
    1049.饭盒
    1092.校门外的树
    1012.区间合并
    1054.数独
    1175.开心的金明 01背包
    空格(Space)的ASCII码值是:32
    js为lable和div赋值
  • 原文地址:https://www.cnblogs.com/liuxiaolu/p/9989102.html
Copyright © 2011-2022 走看看