2016-10-07 25 views
-3
の最大数を計算するために同じ外部テキストファイル(organsations.txt)からの情報を使用する方法

organisations.txt:平均、最小値と分

私は、平均、最小&最大値を計算したい
Melbourne:www.melbourne.edu.au 199.45.12.3; 1245 
Famagusta:www.famagusta.com 145.78.35.6; 499 
Athens:www.athens.org.gr 178.55.12.2;  6789 
Istanbul:www.istanbul.com.tr 145.44.32.7; 2980 

2番目の列の数字...どのようなfuction /メソッドを使うべきですか?インターネット上で試したが、関連するものは何もなかった。

コード:

if operation == 2: 
    with open('pass.txt') as f: 
     credentials = dict([x.strip().split(':') for x in f.readlines()]) # Created a dictionary with username:password items 

    username_input = input('Please Enter username: ') 

    if username_input not in credentials: # Check if username is in the credentials dictionary 

     sys.exit('Incorrect incorrect username, terminating... \n') 

    password_input = input('Please Enter Password: ') 

    if password_input != credentials[username_input]: # Check if the password entered matches the password in the dictionary 

     sys.exit('Incorrect Password, terminating... \n') 

    print ('User is logged in!\n') 

    #with open("organisation.txt") as f: 

    with open('organisation.txt') as f: 
     #organisations = dict([x.strip().split(':') for x in f.readlines()]) 
     lines = f.readlines() 
    numbers = [int(line.split(";")[-1].strip()) for line in lines if line.strip()] 
    maxval = str(max(numbers)) 
    minval = str(min(numbers)) 
    print('maximum value:'+maxval) 
    print('maximum value:'+minval) 
+1

あなた自身でこれを書いてみませんか?あなたはどこにいらっしゃいますか?あなたはファイルを読むことができますか?もしそうなら、あなたはこれらの統計をどこに残していますか? – CoryKramer

+0

私は試しましたが、私は値を印刷するときに、行全体が印刷されます。 – Von

+1

あなたが試したことをお見せしましょう。今すぐあなたの質問は "私にこの仕事をするコードを与える"のように見えます。我々は特定の質問に答えるためにここにいます。 「助けてください」という質問は問題ではなく、とにかく答えるほど具体的なものでもありません。 – CoryKramer

答えて

1

私はこれだろう。また

with open("organisation.txt") as f: 
    lines = f.readlines() 

numbers = [int(line.split(";")[-1].strip()) for line in lines if line.strip()] 
maxval = max(numbers) 
minval = min(numbers) 

を平均値のためCalculating arithmetic mean (average) in Pythonを参照してください。

+0

あなたの解決策はありがたいですが、マイナーチェンジの変更:\t \t maxval = str(max(numbers)) minval = str(min(numbers)) – Von

関連する問題