python--分支和assert断言
【摘要】
python--分支和assert断言
#分支和assert
score = int(input('请输入一个分数'))
if score == 100:
print('成绩为A')
elif 99>=score>=90:
print('成绩为B')
...
python--分支和assert断言
#分支和assert
score = int(input('请输入一个分数'))
if score == 100:
print('成绩为A')
elif 99>=score>=90:
print('成绩为B')
elif 89>=score>=80:
print('成绩为C')
elif 79>=score>=60:
print('成绩为D')
elif score<59:
print('成绩为不及格')
else:
print('输入的成绩不正确')
# assert(断言)当这个关键字后边的条件为假的时候,程序自动崩溃并抛出AssertionError的异常
assert 3<4
这里介绍几个常用断言的使用方法,可以一定程度上帮助大家对预期结果进行判断。-
assertEqual
assertNotEqual
assertTrue
assertFalse
assertIsNone
assertIsNotNone
assertEqual 和 assertNotEqual
assertEqual
:如两个值相等,则pass
assertNotEqual
:如两个值不相等,则pass
使用方法:assertEqual(first,second,msg)
其中first
与second
进行比较,如果相等则通过;msg
为失败时打印的信息,选填;断言assertNotEqual反着用就可以了。
assertTrue和assertFalse
assertTrue
:判断bool值为True,则pass
assertFalse
:判断bool值为False,则Pass
使用方法:assertTrue(expr,msg)
其中express
输入相应表达式,如果表达式为真,则pass;msg
选填;断言assertFalse
如果表达式为假,则pass
assertIsNone和assertIsNotNone
assertIsNone:不存在,则pass
assertIsNotNone:存在,则pass
使用方法:assertIsNone(obj,msg)
检查某个元素是否存在
-
assertEqual(a, b) a == b
-
assertNotEqual(a, b) a != b
-
assertTrue(x) bool(x) is True
-
assertFalse(x) bool(x) is False
-
assertIs(a, b) a is b 2.7
-
assertIsNot(a, b) a is not b 2.7
-
assertIsNone(x) x is None 2.7
-
assertIsNotNone(x) x is not None 2.7
-
assertIn(a, b) a in b 2.7
-
assertNotIn(a, b) a not in b 2.7
-
assertIsInstance(a, b) isinstance(a, b) 2.7
-
assertNotIsInstance(a, b) not isinstance(a, b) 2.7
-
-
assertAlmostEqual(a, b) round(a-b, 7) == 0
-
assertNotAlmostEqual(a, b) round(a-b, 7) != 0
-
assertGreater(a, b) a > b 2.7
-
assertGreaterEqual(a, b) a >= b 2.7
-
assertLess(a, b) a < b 2.7
-
assertLessEqual(a, b) a <= b 2.7
-
assertRegexpMatches(s, re) regex.search(s) 2.7
-
assertNotRegexpMatches(s, re) not regex.search(s) 2.7
-
assertItemsEqual(a, b) sorted(a) == sorted(b) and works with unhashable objs 2.7
-
assertDictContainsSubset(a, b) all the key/value pairs in a exist in b 2.7
-
assertMultiLineEqual(a, b) strings 2.7
-
assertSequenceEqual(a, b) sequences 2.7
-
assertListEqual(a, b) lists 2.7
-
assertTupleEqual(a, b) tuples 2.7
-
assertSetEqual(a, b) sets or frozensets 2.7
-
assertDictEqual(a, b) dicts 2.7
-
assertMultiLineEqual(a, b) strings 2.7
-
assertSequenceEqual(a, b) sequences 2.7
-
assertListEqual(a, b) lists 2.7
-
assertTupleEqual(a, b) tuples 2.7
-
assertSetEqual(a, b) sets or frozensets 2.7assertDictEqual(a, b) dicts 2.7
文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/80311058
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)