Day1:If else流程判断

一、if...else语句

  if 条件成立:

    执行条件成立后的代码

  else:

    执行条件不成立的代码

注:注意有冒号,python会强制缩进!一般语句都必须顶格写,缩进是缩进一个tab键,等于4个空格

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:Hiuhung Wan
 4 _username = "Hiuhung Wan"
 5 _password = "abcd1234"
 6 username = input("Username:")
 7 password = input("Password:")
 8 if _username == username and _password == password:
 9     print("Welcome user {name} login...".format(name = username))
10 else:
11     print("Invalid username or password!")
View Code

二、if...elif...else语句  

  if 表达式1:
    语句1
  elif 表达式2 :
    语句2
  elif 表达式3 :
    语句3
  elif 表达式m :
    语句m
  else:
    语句

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:Hiuhung Wan
 4 age_of_MrWang = 48
 5 guess_age = int(input("Enter the age of Mr Wang:"))
 6 if guess_age == age_of_MrWang:
 7     print("Yes,you got it!")
 8 elif guess_age < age_of_MrWang:
 9     print("Think bigger!")
10 else:
11     print("Think smaller!")
View Code
原文地址:https://www.cnblogs.com/hiuhungwan/p/7674359.html