gpt4 book ai didi

python - 可以在asserts的消息中引入变量的输出吗

转载 作者:行者123 更新时间:2023-12-04 11:58:19 24 4
gpt4 key购买 nike

我正在对我创建的一个程序的输入数据进行一些验证。我正在用断言来做这件事。如果出现断言,我想知道数据的哪一部分出现,所以我想获得出现断言的值。

assert all(isinstance(e, int)
for l1 in sequence.values()
for l2 in l1 for e in l2),"Values of the dictionnary aren't lists of integers. Assert found in '{l2}'"
# This code doesn't work

最佳答案

不使用 all因为它没有公开“迭代变量”。您将需要一个显式的嵌套循环。你也忘记了 f''表示 f 字符串的前缀:

for l1 in [['a']]:
for l2 in l1:
for e in l2:
assert isinstance(e, int), f"Values of the dictionnary
aren't lists of integers. Assert found in '{l2}'"
AssertionError: Values of the dictionnary aren't lists of integers. Assert found in  'a'

关于python - 可以在asserts的消息中引入变量的输出吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69392936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com