我的代码有问题。 1-黄金 2-银色 3-青铜
我想做的是计算每年获得多少块金牌。例如,在2002年,共有2枚金牌、1枚银牌和1枚铜牌。你知道吗
代码:
def main():
year = str(input("Enter year to count its winners: "))
goldmedal = 0
openFile = open("test.txt")
gold = "1"
for line in openFile.read().split('\n'):
if year in line:
if str(1) in line:
goldmedal = goldmedal + 1
print("Gold Medals: ", gold medal)
预期产量:
Enter year to count its winners: 2002
Gold Medals: 2
文本文件:
WHEELER
ADAM
2001
3
KHUSHTOV
ASLANBEK
2002
1
LOPEZ
MIJAIN
2002
1
BAROEV
KHASAN
2002
2
BAROEV
KHASAN
2002
3
一个简单的修复程序将使用迭代器并请求下一行:
一个合适的解决方案是用
'\n\n'
分割块,并将这些块作为一组关于某个奖牌的数据进行处理。你知道吗此代码是一个错误(tm)修复程序—它假定数据是正确的,并在找到正确的年份时设置标志。这不是一个好的解决方案:更好的解决方案(在我看来)是先将数据加载到数据结构中(可能是一个字典),然后搜索字典
更好的解决方案是:
注意:这些应该是好的,但我也没有测试。你知道吗
相关问题 更多 >
编程相关推荐