# if.py
pwd = input('What is the password? ')
if pwd == 'apple':
print('Logging on ...')
else:
print('Incorrect password.')
运行结果:
>>>
What is the password? banana
Incorrect password.
>>>
What is the password? apple
Logging on ...
--elif
if 条件:
表达式
elif 条件:
表达式
else:
表达式
--条件表达式
# if1.py
food = input('What is your favorite food? ')
reply = 'Yes' if food == 'apple' else 'No'
print(reply)
运行结果:
>>>
What is your favorite food? apple
Yes
>>>
What is your favorite food? banana
No