Problems with 'while' loop and 'for' loop when reading through file
- by David Beckham
I wasted plenty of hours trying to figure out the problem but no luck. Tried asking the TA at my school, but he was useless. I am a beginner and I know there are a lot of mistakes in it, so it would be great if I can get some detail explanation as well. Anyways, basically what I am trying to do with the following function is:
Use while loop to check and see if random_string is in TEXT, if not
return NoneType
if yes, then use a for loop to read lines from that TEXT and put it
in list, l1.
then, write an if statement to see if random_string is in l1.
if it is, then do some calculations.
else read the next line
Finally, return the calculations as a whole.
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