def list():
list_name = []
list_name_second = []
with open('CoinCount.txt', 'r', encoding='utf-8') as csvfile:
num_lines = 0
for line in csvfile:
num_lines = num_lines + 1
i = 0
while i < num_lines:
for x in volunteers[i].name:
if x not in list_name: # l
f = 0
while f < num_lines:
addition = []
if volunteers[f].true_count == "Y":
addition.append(1)
else:
addition.append(0)
f = f + 1
if f == num_lines:
decimal = sum(addition) / len(addition)
d = decimal * 100
percentage = float("{0:.2f}".format(d))
list_name_second.append({'Name': x , 'percentage': str(percentage)})
list_name.append(x)
i = i + 1
if i == num_lines:
def sort_percentages(list_name_second):
return list_name_second.get('percentage')
print(list_name_second, end='\n\n')
上面是我的一段代码,它的意思是:
如果第 n 行名称中的字符串尚未列出,请找出准确计数的硬币百分比,然后将其全部添加到列表中,然后打印该列表。
问题是,当我输出这个时,程序在 addition.append(1)
上连续卡在一个 while 循环中,我不知道为什么,所以请你(使用显示的代码)让我知道如何更新使其按预期运行的代码,如果有帮助,txt 文件中的前两行代码读取:
Abena,5p,325.00,Y
Malcolm,1p,3356.00,N
这无关紧要,但只是以防万一您需要它,我怀疑它卡在循环 addition.append(1)
的原因是因为第一行有一个 "Y"
作为它的 true_count
< /p>