-1

I am working on an assignment for school. I've emailed my professor and got very vague non helpful answers. Below is what I have to do

Write and test a program to meet the following specifications:

Write a function called fileToList(inFile) where inFile is a file that has 7 lines with one number per line. The content of inFile is transferred to a list called numbers. The function will return the list called numbers that was created in the function (30 points).

Write a function called sumList(nums) where nums is a list of numbers. The function will return the sum of the numbers in the list (30 points).

Write a function called main() that calls the functions fileToList(inFile) and sumList(nums) and prints the result of sumList(nums) (30 points).

Include a header at the top of the code with the following information (5 points):

# Name of programmer: you name goes here
# Date: date program was written
# Description: a description of what the program does. 

Use the file data.txt for your file that is read by the function fileToList(inFile).

#SumList.py
# 02/26/2017
# This program will pull data from a file prints the data then sums the numeric data.



inFile = open("data.txt","r")
nums = []

def fileToList(inFile):
    numbers = []
for i in range(7):
    numbers.append(inFile.readline())
print('\n'.join(numbers))


def sumList(nums):
    file = open("data.txt","r")

    line = file.read()

    total = sum(file)

print(total)

def main():
    fileToList(inFile)
    sumList(nums)
main()

the data file

1245.67
1189.55
1098.72
1456.88
2109.34
1987.55
1872.36
14mC4
  • 27
  • 4
  • I'm sorry, but what is your question, exactly? This isn't a tutoring service, but we can help you if you post a *specific question*. Also, the indentation here is definitely not correct. Is that how you've actually indented? – juanpa.arrivillaga Feb 27 '17 at 23:14
  • You may want to read [How to ask homework questions](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) before posting again. – OneCricketeer Feb 27 '17 at 23:34
  • I cant stand when people refer you to "How to ask questions..." because they know very well they did the same thing at the beginning of their programming journey and it helps nobody... – pstatix Feb 27 '17 at 23:45
  • @pstatix It's a homework question. There is a fine line in "acceptable formats" for those. (like *Never use code you don't understand*). And it is encouraged for everyone to go to the help center to see what is "on-topic" or not before posting any quesiton. – OneCricketeer Feb 28 '17 at 00:00
  • yea and have that stuff is full of over-done code. It's not helpful at all for entry level – 14mC4 Feb 28 '17 at 00:08
  • @cricket_007 then at least point him the right direction. Don't blanket it to get kudos. You've still done nothing to encourage a solution. – pstatix Feb 28 '17 at 03:20
  • @pstatix Excuse me? I did give an answer – OneCricketeer Feb 28 '17 at 03:23

3 Answers3

0

The function will return the list called numbers that was created in the function

You need to return, not print.

def fileToList(inFile):
    numbers = [int(line) for line in inFile.readlines()]
    return numbers

Write a function called sumList(nums) where nums is a list of numbers. The function will return the sum of the numbers in the list

Again, return is missing... And there is no file that should be open()-ed... Please read How to sum a list of numbers in python

Write a function called main() that calls the functions fileToList(inFile) and sumList(nums) and prints the result of sumList(nums)

You need to get the return values from the functions.

It is part of your assignment to figure that out, but here is a start

def main():
    with open("data.txt") as inFile:
        numbers = fileToList(inFile)

In much less code: Python: How to sum numbers from text file

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Update:

use .strip() to remove hidden characters/spaces around the row, and use convert_2_decimal_float() converting string to float value with 2 decimal precision.

def fileToList(inFile):
    numbers = []
    for row in inFile:
        float_value = convert_2_decimal_float(row.strip())
        print(float_value)
        numbers.append(float_value)
    print(','.join([str(n) for n in numbers]))
    return numbers


def sumList(nums):
    print(convert_2_decimal_float(sum(nums)))

def convert_2_decimal_float(v):
    return round(float(v), 2)

def main():
    inFile = open("sample.csv")
    nums = fileToList(inFile)
    sumList(nums)
    inFile.close()
main()

Open the file in method main(). iterate inFile in fileToList(), add numbers in list numbers and returns the list, next step pass the returned list into sumList() and sum up all numbers by built-in method sum(), in the end close the opened file

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
-1

When reading the lines in your file you want to be converting the values to integers like so: numbers.append(int(inFile.readline()))

You also want your fileToList function to actually return the list of numbers so you can use the list in main, not just print them therefore your fileToList function should look like:

def fileToList(inFile):
    numbers = []
    for i in range(7):
        numbers.append(float (inFile.readline()))
    return numbers

The sumList function can be a simple function that takes the list of numbers as a parameter and returns the sum:

def sumList(numbers):
    return sum(numbers)

Therefore in main you can call fileToList and store the list of numbers returned in a variable:

def main():
    numbers = fileToList(inFile)

And then use the numbers as a parameter into the sumList function and print the result:

    print(sumList(numbers))

Program:

inFile = open("data.txt","r")

def fileToList(inFile):
    numbers = []
    for i in range(7):
        numbers.append(float(inFile.readline()))
    inFile.close()
    return numbers

def sumList(numbers):
    return sum(numbers)

def main():
    numbers = fileToList(inFile)
    print(sumList(numbers))

main()
Adam
  • 131
  • 7
  • If something is incorrect or not useful then please tell me but why the downvote otherwise? – Adam Feb 27 '17 at 23:33
  • it gives me a invalid literal for int() with base 10: '1245.67\n – 14mC4 Feb 28 '17 at 01:29
  • Oh my bad, i wrongly assumed thst the data file consisted of integers so when I tried it with integer examples it worked, replace `int (inFile.readline ())` with `float (inFile.readline ())` I will edit my answer. – Adam Feb 28 '17 at 07:52