我浪费了很多时间试图找出问题,但没有运气。我在学校里试过TA,但他没用。我是一个初学者,我知道它有很多错误,所以如果我能得到一些细节解释也会很棒。无论如何,基本上我试图用以下功能做的是:
TEXT = open('randomfile.txt')
def random (TEXT, random_string):
while random_string in TEXT:
for lines in TEXT:
l1=TEXT.readline().rsplit()
if random_string in l1:
'''
do some calculations
'''
else:
TEXT.readline() #read next line???
return #calculations
return None
答案 0 :(得分:1)
也许:
def my_func(ccy):
with open('randomfile.txt', 'r') as f:
l1 = [float(line.split()[-1]) for line in f.readlines() if ccy in line]
if l1:
return sum(l1) / len(l1)
else:
return None