Python正课13 —— 流程控制之if判断

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12425826.html

一:缩进

逐行缩进

  if条件:

    代码1

    if条件2:

      代码2

二:语法

语法1:

  if条件:

    代码1

    代码2

    代码3

age = 60
is_beautiful = True
star = '水瓶座'

if age > 16 and age < 20 and is_beautiful and star == '水瓶座':
    print('我喜欢你,我们在一起吧!')

语法2:

  if条件:

    代码1

  else:

    代码1

age = 60
is_beautiful = True
star = '水瓶座'

if age > 16 and age < 20 and is_beautiful and star == '水瓶座':
    print('我喜欢你,我们在一起吧!')
else:
    print('阿姨好,我逗你玩儿呢,深藏功与名')
print('其他代码')

语法3:

  if条件:

    代码1

  elif:

    代码1

  else:

    代码1

score = 63

if score >= 90:
    print('优秀啊,小伙子!')
elif score >= 80:
    print('还可以吧!')
elif score >= 60:
    print('要努力了呀,小伙子')
else:
    print("滚啊!!!")

改进版:

score = input('请输入您的成绩:')
score = int(score)

if score >= 90:
    print('优秀啊,小伙子!')
elif score >= 80:
    print('还可以吧!')
elif score >= 70:
    print('要努力了呀,小伙子')
elif score >= 60:
    print('准备叫家长吧!')
else:
    print("滚啊!!!"

猜你喜欢

转载自www.cnblogs.com/xuexianqi/p/12425903.html
今日推荐