2016-03-27 9 views
-1
def main(): 
    filename = input("Enter a file name: ") 
    with open(filename) as f: 
     data = [int(line) for line in f] 
     if len(data) > 2: 
      print('The smallest number in the file is: ', + min(data)) 
      print('The largest number in the file is: ', "{:,}".format(+ max(data))) 
      print('The total sum of the number in the file is: ', "{:,}".format(sum(data))) 
      print('The average from the numbers in the file is: ', "{:,.2f}".format(sum(data)/len(data))) 
     elif len(data) == 1: 
      print ('There is only one number in your file to process.') 
     else: 
      print('There are no numbers in your file to process.')   
     f.close() 

main() 
+2

さて、質問は何ですか? – Bahrom

+0

エラーメッセージが表示され続けます。ValueError:空のファイルを入力すると、基数10のint()のリテラルが無効です( '\ n')。私はこれを回避する方法がわかりません。 –

+1

これらの詳細を質問に追加してください。 – Bahrom

答えて

0

空の場合は、メッセージを与えます私は、コードの残りの部分を経とロジックを確認していません -

lines = filter(None, (line.strip() for line in f)) 
data = [int(line) for line in lines if line.isnumeric()] 

はこれが唯一のあなたが取得している例外を扱うことに注意してください。それを回避するために、次のようなものを使用することができます。

+0

パーフェクト!それは美しく働いた。本当にありがとう! –

関連する問題

 関連する問題