Python 你在撒谎吗?1 != 1.
我有一段代码Python 你在撒谎吗?1 != 1.,python,django,assertions,Python,Django,Assertions,我有一段代码 assert (len(max_test_scores) != 1), \ "Internal error - migration 0011 - module Programs." \ " Please contact with developers - " + str(len(max_test_scores)) 在执行这段代码的过程中,我遇到了一个断言错误: AssertionError:内部错误-迁移0011-模块程序。请与开发者
assert (len(max_test_scores) != 1), \
"Internal error - migration 0011 - module Programs." \
" Please contact with developers - " + str(len(max_test_scores))
在执行这段代码的过程中,我遇到了一个断言错误:
AssertionError:内部错误-迁移0011-模块程序。请与开发者联系-1
那么1!=1 ?
我在搜索,在谷歌上搜索,想这是怎么发生的,但我不知道。下面是代码及其上下文
def forwards(self, orm):
problems_and_groups = orm.Test.objects \
.values('problem', 'group').distinct()
problems_instances_and_groups = []
for pi in orm['contests.ProblemInstance'].objects.all():
for pg in problems_and_groups:
if pi.problem.pk == pg['problem']:
problems_instances_and_groups \
.append({'problem_instance': pi.pk,
'group': pg['group']})
count = len(problems_instances_and_groups)
num = 0
update_every = max(1, count / 20)
pb = ProgressBar(count)
print "Migrating %d groups" % count
for pig in problems_instances_and_groups:
if num % update_every == 0:
pb.update(num)
num += 1
submissions = orm['contests.Submission'].objects \
.filter(problem_instance = pig['problem_instance']) \
.values('pk')
print submissions
submission_reports = orm['contests.SubmissionReport'].objects \
.filter(submission__in = submissions).values('pk')
print submission_reports
test_reports = orm.TestReport.objects \
.filter(test_group = pig['group'],
submission_report__in = submission_reports)
max_score = None
max_test_scores = frozenset(test_report.test.max_score
for test_report in test_reports)
assert (len(max_test_scores) != 1), \
"Internal error - migration 0011 - module Programs." \
" Please contact with developers - " + str(len(max_test_scores))
max_score = ScoreValue(list(max_test_scores)[0])
GroupReport.filter(test_group=pig['group'],
submission_report__in = submission_reports) \
.update(max_score=max_score)
如果断言为False
,则抛出一个AssertionError
在这种情况下,显然是这样的,因为在您的示例中,len(max_test_scores)
返回1
(如assert
错误消息中所示,1!=1
是False
如果断言是False
,则抛出AssertionError
在这种情况下,显然是这样,因为在您的示例中,
len(max_test_scores)
返回1
(如assert
错误消息中所示,1!=1
为False
当表达式不为真时,断言失败:
表达式不为true时,断言失败:
你在哪里证明
len(max\u test\u scores)
不等于1
?@LukasGraf在断言消息中。@MatthewTrevor是的,因为str(len(max\u test\u scores))等于1@bdfhjk这就是你所做的断言。它失败了。所以len(max_test_分数)
显然等于1
。但是你告诉我们它不是,因此“Python在撒谎”。而且你的示例没有包含必要的数据来说明其他情况。好了,伙计们,我知道问题出在哪里了:)你在哪里证明len(最大测试分数)
不等于断言消息中的1
?@LukasGraf@MatthewTrevor是,因为str(len(max_test_scores))等于1@bdfhjk这是您所做的断言。它失败了。因此len(max\u test\u scores)
显然等于1
。但您告诉我们它不是,因此“Python在撒谎”.而且你的例子没有包含必要的数据来说明其他情况。好了,伙计们,我知道问题出在哪里了:)
>>> val = 1
>>> assert val != 1, 'Oops, val is 1'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: Oops, val is 1
assert len(max_test_scores) == 1, (
"Internal error - migration 0011 - module Programs. "
"Please contact with developers - {}".format(len(max_test_scores)))