I am writing Python code which reads through two text files and prints the output based on those two files. Basically, the first file, demo.txt, has a matrix of numbers and the second file, days.txt, has a value. Based on that value, many digits in the first file are replaced with the
?, or just a space.
Anyways, my code is working perfectly, but I would like to simplify it in a way that it is easier to understand for very new programmers. So you can break it into many parts, change the list comprehensions on line 17. Also, can we break up the with statement on line 12 as well?
def repl(ch,threshold): if ch.isdigit(): if int(ch) < threshold: return '$' elif int(ch)> threshold: return " " else: return ch else: return '?' lines =  with open('data') as f, open("data.txt","r") as f2: threshold = int(f2.read().split('=')[-1]) for line in f: line = line.strip() line = line.split() line = [repl(ch,threshold) for ch in line] lines.append(line) # reset to start of demo.txt f.seek(0) for line in f: print(line) for line in lines: print() for el in line: print(el,end=" ")