我創建一個函數讀取.txt
文件,該文件包含以下內容:平均通過str .txt文件的和int內它
Testing.txt:
a 1 34
b 2 25
c 3 23
j 4 80
我需要創建一個函數讀取此文件並打印文件中每行的最後2位數的平均值。
我到目前爲止是這樣的:
def Stats():
filename = input("Enter a file name: ")
with open(filename) as f:
data = [int(line) for line in f]
print("The average value is ", sum(data)/len(data))
Stats()
優點:
- 這個函數將在函數內部產生INT值的平均值。
缺點:
- 如果該文件包含在它的STR它不會工作。
問題:如何讓我的函數能夠使用上面列出的名爲(testing.txt)的文件?
由於某種原因,我仍然得到這個錯誤。 – stacker 2015-04-05 01:32:40
你會得到什麼錯誤? – Marcin 2015-04-05 01:35:22