本文共 816 字,大约阅读时间需要 2 分钟。
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 MasterPlease input password: aaaWrong password!Please input name: master
Hello MasterPlease input password: abc123Access 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 ManInput your age: 1000
Your Majesty
转载地址:http://gwrsx.baihongyu.com/