0

I am working on a homework assignment for introductory Python, so I don't want an outright answer, just clarification. My question states to implement a function to ask the user for a list of numbers, then to check if the number is greater than another value and if so, to write those values to a file. I believe what I have written is correct but I am unsure how to check it. Below is the code I have written:

def numberLogger (filename,minval):
    'ask the user to enter a list of numbers.  If a number is greater than or equal to the second paramter, append it to a file'
    userdata=input('Enter a series of numbers seperated by a comma: ')
    u=userdata.split(',')
    for i in u:
        i=int(i)
        if i>=minval:
            infile=open(filename,'w')
            infile.write(i)
            infile.close()

Any help is much appreciated, as I said, it is homework so please don't just give away the answer, rather guide me to it.

7
  • Run the program and then enter a series of number separated by a comma, as the prompt says? It looks fine to me. Commented Feb 1, 2022 at 2:41
  • I did that but once I opened the file to check, the original values were still there Commented Feb 1, 2022 at 2:42
  • 2
    I would definitely open the file once and close it at the end of your loop, rather than open and close it on each item that matches your criteria. Commented Feb 1, 2022 at 2:43
  • This code doesn't replace anything; it writes a new file. Commented Feb 1, 2022 at 2:53
  • @JohnGordon My instructor's notes say that the write function for files will delete the values that are in the opened file and replace it with whatever is written, as long as no new filename is specified. It says the following: To write to a file, the file must be opened for writing: >>> outfile = open('out.txt', 'w') How does this work? • If there is no file out.txt in the current working directory, the above open() function will create it. • If a file out.txt exists, then its contents will be truncated (erased). • In both cases, the cursor will point to the beginning of the file. Commented Feb 1, 2022 at 2:55

1 Answer 1

1

To clean this up, I would do a few things. Only open the file once.

def numberLogger(filename, minval):
    userdata = input('Enter a series of numbers seperated by a comma: ')
    u = userdata.split(',')
    with open(filename, 'w') as infile:
        for i in u:
            i=int(i)
            if i>=minval:
                infile.write(i)

You can also use a generator expression to filter out your data.

def numberLogger(filename, minval):
    userdata = input('Enter a series of numbers seperated by a comma: ')
    u = userdata.split(',')
    with open(filename, 'w') as infile:
        for i in (i for x in u for i in [int(x)] if i >= minval):
            infile.write(i)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.