注释及条件判断(流程控制语句)的练习

注释

  • 给一些晦涩难懂的代码进行标注或解释
  • PEP8,不能超过规定的线
1. 单行注释: # xxx

2. 多行注释: 
    """
    xxx
    """

3. ctrl+/  直接把圈起来的部分改成注释

条件判断(流程控制语句)

  • =是赋值;
  • ==是等于;
  • !=是不等于
  • :意味着语句结束
  • 缩进:4个空格=tab
  • tab不能和space混合使用,不然错误很难找
  • 1个IF,if+else,if+elif+elif, if+elif+else, if嵌套, if+if+if+if
  1. sex=input("请输入你的性别:")
    if sex == "girl":    
     print("come on,baby!")
    else:    
     print("go and find Xiaoming Huang, he is the same guy with you")
  2. sex=input("请输入你的性别:")
    if sex == "man":    
     print("bye")
    elif sex == "girl":    
     print("come on,baby!")
    else:    
     print("go and find Xiaoming Huang, he is the same guy with you")
  3. sex=input("请输入你的性别:")  man
    if sex == "girl":    
     print("come on,baby!")
    • if语句不是必须要跟else,如3.就可以不输出。
  4. practice

    num=input("请输入你的年龄:")
    number=int(num)
    if number > 18 and number < 25:
        print("come on,baby!")
    else:
        print("That's not good")
    
    
    name=input("请输入你的名字:")
    password=input("请输入你的密码:")
    if name != "alex" or password != "oldboy":
        print("There is some wrong with your name or password!")
    else:
        print("land successfully")
    
    
    sex = input("请输入你的性别:")
    if sex == "man":
        print("go out and fuck yourself")
    else:
        age = int(input("请输入你的年龄:"))
        if age <= 25:
            print("hello")
        else:
            print("sorry,i`m so tired.")
    print("that`s a good night")
    • != 为 py3 中的不等号
    • 若存在else,则必定会有一个输出,即“多选一”

  5. if 嵌套

    • 如果 条件:

    • 缩进 如果 条件:

      ​ 缩进 结果

  6. if if if

    常量

    • 常量:变量名大写的就是常量。(非python中的定义,python中不存在常量)eg:ID
    • 可以修改,但尽量别改
    • 用于配置文件中

猜你喜欢

转载自www.cnblogs.com/Guoxing-Z/p/11469525.html