这段代码工作正常,但有没有办法用循环减少重复代码?
我知道这是非常基本的。我用过Python2.7
if List[3] == answer[3]:
self.score = int(self.score) + 1
self.questions = self.questions + 1
info = info + 1
self.update()
else:
self.questions = self.questions + 1
self.update()
if List[4] == answer[4]:
self.score = int(self.score) + 1
self.questions = self.questions + 1
info = info + 1
self.update()
else:
self.questions = self.questions + 1
self.update()
如果有办法使用循环?这些列表都是从[0]
到[5]
答案 0 :(得分:2)
for x, y in zip(List, answer):
if x == y:
self.score = int(self.score) + 1
self.questions = self.questions + 1
info = info + 1
self.update()
else:
self.questions = self.questions + 1
self.update()
答案 1 :(得分:0)
应该很简单:
for i in range(5 + 1):
if List[i] == answer[i]:
self.score = int(self.score) + 1
self.questions = self.questions + 1
info = info + 1
self.update()
else:
self.questions = self.questions + 1
self.update()