age = 14
if age>=18:
print('enough to vote') #判断语句
print('have you registered?')
else:
print('sorry you cannot vote')
if-elif-else语句:
if age < 4:
print("cost is 0")
elif age<18: #提供其他选项
print('cost is 5')
else:
print("cost is 10")
或者先给变量赋值,最后进行输出
if age < 4:
price = 0
elif age<18:
price = 5
else:
price = 10
print("Your admission cost is " + str(price) + '.')
如过只想执行一个代码块,就使用if-elif-else结构,如果要运行多个代码块,就使用一系列独立的if语句。