Loading

条件判断语句

python里的条件判断语句 if/ if else/ if elseif elseif else

python 里不支持switch...case条件语句

if...else...

  • if判断语句:

    ​ 条件成立时,执行的代码

  • else:

    ​ 条件不成立时执行的代码

age = int(input("请输入你的年龄"))
if age < 18: # 字符串和数字作比较:==结果是False,!=结果是True,其他比较运算会报错
    print("未满十八岁,禁止进入")
else:
    # 条件不成立时执行的代码
    print("澳门首家线上赌场上线了!")

练习题

写出判断一个数是否能够同时被3和7整除的条件语句,并且打印对应的结果

num = int(input("请输入数字:"))
if (num % 3) == 0 && (num % 7) == 0:
    print('能够被3和7整除')
else:
    print('不能被3和7整除')

if...elif...elif...

多个if语句,语句和语句之间不存在关联

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

if 60 > score >=0:
    print("不及格")
elif 80 > score >= 60:
    print('一般般')
elif 90 > score >= 80:
    print('还不错')
elif 100 >= score >= 90:
    print('好棒吆')
posted @ 2021-02-06 15:15  群哥V  阅读(95)  评论(0)    收藏  举报