python——if...elif...和三元条件表达和asse

2019-06-29  本文已影响0人  ELLENXX

if....elif....

score=int(input('输入你的成绩:'))
if 100>score and score>=90:
    print('you are A')
elif 90>score and score >=80:
    print('you are B')
elif 80>score and score >=70:
    print('you are C')
elif 80>score and score >=60:
    print('you are D')
else:print('重修吧!')

三元条件表达

if x<y:
    small=x
else:
    small=y
print(small)

等价于

x,y=6,2
small=x if x<y else y
print(small)

assert

assert这个关键字我们称为断言,当这个关键字后面的条件为假的时候,程序自动崩溃并抛出AssertionError的异常

assert 3>4

Traceback (most recent call last):
File "......", line 1, in <module>
assert 3>4
AssertionError

我们可以利用assert程序中加入检查点,只有满足assert后面的条件的时候才能让程序正常工作

上一篇 下一篇

猜你喜欢

热点阅读