Python学习之:if条件

# if条件句
# 1、两层判断举例:
# 先看是几重判断,两层判断用:if else
# 大于两层判断,用:if elif else


# 两层判断举例:年龄大于等于18;年龄小于18
age = 17
if age >= 18:
print('你的年龄已经大于18岁!,你已经是个成年人')
else:
print('你的年龄小于18岁,你还是个孩子')

# 2、多层判断举例:
# 成绩大于等于90分,提示:超过90分了,你考的很好!
# 成绩小于90分,大于等于80分,提示:超过80分了,你考的不错,继续努力!
# 成绩小于80分,大于等于60分,提示:刚及格,你考的太一般了!
# 成绩小于60分,提示:你都没及格,以后别玩手机了!

score = 98
if score >= 90:
print('超过90分了,你考的很好!')
elif score >= 80:
print('超过80分了,你考的不错,继续努力!')
elif score >= 60:
print('刚及格,你考的太一般了!')
else:
print('你都没及格,以后别玩手机了!')

# 3、字符串转int类型
score = input('请输入你的成绩:') #raw_input python2
score = int(score) # 类型转换,字符串转成整数类型
score = float(score) # 转成浮点数,小数类型的
# score int(input('请输入你的成绩:'))
# TypeError: '>=' not supported between instances of 'str' and 'int'
# input接收到的输入全都是字符串
if score >= 90:
print('超过90分了,你考的很好!')
elif score >= 80:
print('超过80分了,你考的不错,继续努力!')
elif score >= 60:
print('刚及格,你考的太一般了!')
else:
print('你都没及格,以后别玩手机了!')

# 4、if条件句中的逻辑与and
score = input('请输入你的成绩:') #raw_input python2
score = int(score) # 类型转换,字符串转成整数类型

if score >= 90:
print('超过90分了,你考的很好!')
elif score <90 and score >= 80:
print('超过80分了,你考的不错,继续努力!')
elif score <80 and score >= 60:
print('刚及格,你考的太一般了!')
else:
print('你都没及格,以后别玩手机了!')

# 只写if也是可以的

posted @ 2020-04-09 13:16  滴滴滴滴答  阅读(307)  评论(0编辑  收藏  举报