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

posted @ 2018-03-26 06:29  mclind  阅读(188)  评论(0编辑  收藏  举报