-1

I'm taking my first ever CS class and I have an assignment due Friday. I just wanted someone to check my code and make sure it works/follows the directions.

Instructions:

Write a program that:

1) gets the name of a text file of numbers from the user. Each number in the file is on its own line.

2) reads in those numbers one at a time

3) writes the even numbers to a file named even.txt

4) writes the odd numbers to a file named odd.txt

5) displays to the user the sum of the positive numbers and the count of the negative numbers.

HERE IS WHAT I HAVE

def main():

    #Open text file for reading
    numberFile = open(r'numberFile.txt', 'r')

#Priming read
    number = numberFile.readline()

#Setting up loop to continue reading until
#an empty line is reached
    total = 0
    count = 0
    while number != '':
        number = float(number)                  #convert from string to number
        if number%2 == 0:                       
            evenNumber = open('even.txt', 'w')  #writes even numbers into a file
            evenNumber.write(number + '\n')
        else:
            oddNumber  = open('odd.txt', 'w')   #writes odd numbers into a file
            oddNumber.write(number + '\n')

        for number in numberFile:
            number = float(number)      #convert from string to number

            if number <= 0:             #identify negative numbers
                count +=1               #count negative numbers

            if number >= 0:             #identify positive numbers
                total += number           #sum of positive numbers
        number = numberFile.readline()

    numberFile.close()                  #close file after program is complete
main()
j.davis
  • 5
  • 4
  • 6
    SO is not a code review or homework revision site. If there is a problem with your code, indicate specifically what is not working by showing the invalid output or error code and provide an explanation of what your expected output is. – idjaw Aug 02 '16 at 20:25
  • 1
    http://codereview.stackexchange.com/questions/ask *is* a codereview site, though – Wayne Werner Aug 02 '16 at 20:31
  • 1
    @WayneWerner But they still expect you to ask specific questions, not just post a bunch of code. – Barmar Aug 02 '16 at 20:58
  • Isn't this what the teacher will do when they grade the assignment? Why do you need us to do their job? – Barmar Aug 02 '16 at 20:59
  • @Barmar presumably the OP wants to get a good grade on the assignment and is looking for help from people who more than likely were there at some point in history. – Wayne Werner Aug 02 '16 at 21:17
  • Is zero a negative number? For sure you could just do `if number > 0` for the total, though, since `total += 0` won't alter the total. – Wayne Werner Aug 02 '16 at 21:19
  • you should open the `odd` and `even` file outside the loop and close then at the end – Copperfield Aug 02 '16 at 21:34
  • ".. make sure it works" - you can test that part yourself. – Jongware Aug 02 '16 at 21:55

1 Answers1

0

Although this isn't a code review site, I'll give you some pointers.

  1. You never get a filename from the user - you should probably add that. It will be something like filename = input('Enter filename: ')
  2. You overwrite the even.txt and odd.txt each time you open it with 'w'. Consider using 'a+'
  3. You never output the total or the count. Try using print on those.

On top of all of that, there are better ways to open files are do those kinds of operations, but I'll let you learn those in a future class.

Brian
  • 1,659
  • 12
  • 17