#python连载第11篇 if 语句
#if单个选择测试,两个print是一个代码块,所以都要执行
age = 17
if age > 10:
print("you are old enough to enjoy love with TA")
print("you can do some wonderful things")
#if一个测试,多个选择语句例子
age = 150
if age < 50:
print("the train ticket is free")
elif age == 50:
print("the train ticket is free")
elif age < 120:
print("the train ticket is half price")
elif age == 120:
print("the train ticket is half price")
else:
print("the train ticket is full price") #身高大于120cm,所以全价票
#if 嵌套
num = int(input("please input an integer:"))
if num%5 == 0:
if num%6 == 0:
print ("the number you input can be exact divided by 5 and 6")
else:
print ("the number you input can be exact divided by 5,but not by 6")
else:
if num%6 == 0:
print ("the number you input can be exact divided by 6,but not by 5")
else:
print ("the number you input can not be exact divided by 5 and 6")
2017-11-26 16:56:48 November Sunday the 47 week, the 330 day
#if的例子,检查字符串
if 'python' == 'Python':
print("'python' == 'Python'")
else:
print("'python' != 'Python'") #不相等哈,字母有大小写分别
if 'love' in "I love you": #in 包含的意思,一个小东西是否在大东西里面
print("'love' is in 'I love you'")
else:
print("'love' is not in 'I love you'")
if 'leben' not in "I love you": #not in 不包含的意思,小数据是否不在大数据里
print("'leben' is not in 'I love you'")
else:
print("'leben' is in 'I love you'")
#检查数字:
if 10 <= 6:
print("10 <= 6 is true")
else:
print("10 <= 6 is false")
#检查多个条件
if 10 > 6 and 10 >9: #and 代表并且,两个条件都成立才是真
print('10 > 6 and 10 >9 is true')
else:
print("10 > 6 and 10 >9 is false")
if 10 > 6 or 10 < 9: #or代表或,有一个条件成立就是真
print('10 > 6 or 10 >9 is true')
else:
print("10 > 6 or 10 >9 is false")
python连载第11篇 if 语句
最新推荐文章于 2022-07-27 08:40:02 发布