当我测试一个计数器时,我发现它似乎只显示最后一个通过它的项目。例如,如果某个东西是优秀的,那么它显示为“1”。但是,不管其他数据如何,其余的都是0。你知道吗
def mealrating(score, review):
for x in range(0,len(score)):
mp = 0
mg = 0
me = 0
if score[x] >= 1 and score[x] <= 3:
review.append("poor")
mp = mp + 1
if score[x] >= 4 and score[x] <= 6:
review.append("good")
mg = mg + 1
if score[x] >= 7 and score[x] <= 10:
review.append("excellent")
me = me + 1
print("The customer rated tonight's meal as:")
print('Poor:' + str(mp))
print('Good:' + str(mg))
print('Excellent:' + str(me))
print("\n")
在每次迭代中,您都要重置mp、mg和me。你知道吗
必须在循环外初始化计数器:
否则每次迭代都会被重置!要使代码更具python风格,请考虑以下提示:
x >= i and x <= j
的条件可以更简洁地写成i <= x <= j
elif
+=
递增变量这就是我的意思:
相关问题 更多 >
编程相关推荐