I was looking around but I couldn’t find a question similar that also includes functions.
My PYTHON code is supposed to output every line in a text file, but in uppercase. I understand how you’d do this without functions, but I have to use a function.
It prints out everything in uppercase, but the first line, I think its because of the f.read ?? I’ve played around with it, but then it just outputs nothing. I also tried a different method, but it created an entirely new file, and that’s not the result I need. Any ideas on how to go about this?
Here’s what I did
def uppercase(): with open("qwerty.txt", "r") as f: for line in f: line = f.read() line = line.upper() return line print(uppercase())
You don’t need to do
line = f.read() since you’re already getting the line with your loop variable. This is why your program doesn’t output the first line.
Moreover, the return is outside the loop so that means you will only return the last value of
line. My guess is that your function should directly print all the lines in uppercase, it’s ok if it returns nothing.
So basically, you should end up with a function like this:
def uppercase(file): with open(file, "r") as f: for line in f: line = line.upper() print(line) uppercase("query.txt")