2017-02-27 49 views
0

我正在爲學校做作業。我發郵件給我的教授,並得到非常模糊的無用答案。下面是我必須做的我需要幫助搞清楚我是否做錯了

編寫和測試方案,以滿足以下規格:

編寫一個叫做fileToList(INFILE)函數,其中INFILE是一個文件, 有7行,每一個號碼線。 inFile的內容是 轉移到名爲數字的列表。該函數將返回在該函數中創建的名爲數字的 列表(30分)。

編寫一個名爲sumList(nums)的函數,其中nums是一個數字列表。 該功能將返回列表中的數字總和(30 分)。

編寫一個名爲main()的函數,調用函數 fileToList(inFile)和sumList(nums),並打印sumList(nums)(30分)的結果 。

包含在以下信息代碼的頂部 (5分)頭:

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

使用Data.txt文件爲您的文件,該文件是由函數 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() 

數據文件

1245.67 
1189.55 
1098.72 
1456.88 
2109.34 
1987.55 
1872.36 
+0

對不起,但是你的問題到底是什麼?這不是一項輔導服務,但如果您發佈*特定問題*,我們可以爲您提供幫助。此外,這裏的縮進絕對不正確。這是你如何縮進? –

+0

你可能想在閱讀之前閱讀[如何問作業問題](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions)。 –

+0

當人們向你推薦「如何提問......」時,我無法忍受,因爲他們非常清楚他們在編程之旅的開始時做了同樣的事情,並且它幫助沒有人...... – pstatix

回答

-1

當讀取你的文件中的行要進行轉換的數值爲整數,像這樣: numbers.append(int(inFile.readline()))

您還希望fileToList功能要實際返回數字列表,以便您可以使用主列表中的列表,而不僅僅是打印它們,因此您的fileToList函數應該如下所示:

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

的sumList功能可以是一個簡單的函數,它的數字作爲參數列表,返回總和:

def sumList(numbers): 
    return sum(numbers) 

因此,在主,你可以打電話fileToList並存儲在返回號碼列表一個變量:

def main(): 
    numbers = fileToList(inFile) 

然後使用數字作爲參數進sumList功能和打印結果:

print(sumList(numbers)) 

計劃:

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() 
+0

如果有什麼不正確或沒有用,請告訴我,但爲什麼downvote,否則? – Adam

+0

它給了我一個無效的字面值爲int()與基數10:'1245.67 \ n – Yrroth

+0

哦,我的壞,我錯誤地認爲數據文件由整數組成,所以當我嘗試它與整數例子它的工作,取代'int(inFile .readline())'with'float(inFile.readline())'我將編輯我的答案。 – Adam

0

功能將返回稱那是在功能

您需要返回創建數字名單,不能打印。

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

編寫一個叫做sumList(NUMS)函數,其中NUMS是號碼清單。該功能將返回數字的總和在列表

再次,return丟失...而且也沒有文件,應該是open() -ed ......請閱讀 How to sum a list of numbers in python

編寫一個叫做main()函數調用函數fileToList(INFILE)和sumList(NUMS)並打印sumList的結果(NUMS)

您甲腎上腺素編輯以獲取函數的return值。

這是你的任務的一部分,以找出答案,但這裏是一個開始

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

在更少的代碼:Python: How to sum numbers from text file

0

更新

使用.strip()刪除隱藏字符/空格圍繞row,並使用convert_2_decimal_float()將字符串轉換爲具有2位小數精度的浮點值。

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() 

用方法main()打開文件。 迭代​​在fileToList(),在列表numbers數字相加,並返回列表,下一步將返回的列表爲sumList(),並通過內置的方法sum()總結所有的數字,到底關閉打開的文件

+0

@ cricket_007不,這不是,'numbers'是呀,對不起,以爲打印寄信人 – haifzhan

+0

列表數據文件的樣本?你的數據文件可能有不包含數字的字符 –

+0

@ cricket_007沒有問題:) – haifzhan

相關問題