码迷,mamicode.com
首页 > 编程语言 > 详细

Python - 基础知识 - 条件判断

时间:2015-10-24 15:32:25      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

1. 简单的if/else条件判断

judge_flow.py

name = input("Please input name: ")
if name == master:
    print(Hello Master)
    password = input(Please input password: )
    if password == abc123:
        print(Access granted.)
    else:
        print(Wrong password!)
else:
    print(Invalid user!)

运行结果:

Please input name: david
Invalid user!

Please input name: master
Hello Master
Please input password: aaa
Wrong password!

Please input name: master
Hello Master
Please input password: abc123
Access granted.

 

2. 多项条件判断

    elif statements

judge_flow_multiple.py

age = int(input("Input your age: "))
if age < 5:
    print(Hi, Baby.)
elif age < 12:
    print(Hi Child.)
elif age < 100:
    print(Hi Man)
else:
    print(Your Majesty)

运行结果:

Input your age: 3
Hi, Baby.

Input your age: 90
Hi Man

Input your age: 1000
Your Majesty

 

Python - 基础知识 - 条件判断

标签:

原文地址:http://www.cnblogs.com/davidgu/p/4906832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!