zoukankan      html  css  js  c++  java
  • Python04:简单if逻辑判断

    密码校验(简单if判断):

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #Author:Mclind

    _username = "mc"
    _password="123"
    username = input("username:")
    password = input("password:")

    if _username == username and _password == password:
        print("Welcome user {name} login...".format(name=username))
    else:
        print("Invalid username or password!")

    输出:

    username:mc

    password:abc

    Invalid username or password!

    username:mc

    password:123

    Welcome user mc login...

    解释:

    If…else…实现逻辑判断,if后边跟判断条件,成功则执行if后的缩进语句,否则执行else后的缩进语句

    关于python语句缩进,在python中不需要程序代码块(如{}),也不需要结束的标记(如fi结束if语句),python中用缩进控制代码块,结束的位置,是强制的缩进。相同的缩进代表相同的执行级别,不缩进就要顶格写。不顶格写就会报错(print输出语句空了一个空格,没有顶格写):

    if _username == username and _password == password:
        print("Welcome user {name} login...".format(name=username))
    else:
        print("Invalid username or password!")

     print ("123")

    报错:

      File "E:/python_code/s14/day01/password.py", line 15

        print ("123")

                    ^

    IndentationError: unindent does not match any outer indentation level

  • 相关阅读:
    1270. 数列区间最大值(climits用法+线段树模板题)
    JDBC&DBCP总结
    1264. 动态求连续区间和(树状数组模板题)
    788. 逆序对的数量(归并排序的应用)
    归并排序(模板题)
    ZoomEye技巧
    工具或安全监测网站(不定时更新)
    bp截包
    CTF/web
    CTF/stega——图片隐写
  • 原文地址:https://www.cnblogs.com/mclind/p/8648400.html
Copyright © 2011-2022 走看看