-3

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

I want to calculate mean, minimum & maximum of the numbers in the second column...what fuction/method should I use? Tried finding on net but nothing relevant was available.

Code:

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
Von
  • 119
  • 1
  • 1
  • 9
  • 1
    Why don't you try writing this yourself? Where exactly are you stuck? Are you able to read the file? If so, where are you stuck computing those statistics? – Cory Kramer Oct 07 '16 at 11:14
  • i tried,but when i print the values the whole line gets printed. – Von Oct 07 '16 at 11:37
  • 1
    Then show us what you tried. Right now your question looks like "give me code that does this task". We are here to help answer specific questions, not to have work farmed out to us. "Please help" is not a question, nor is it specific enough to answer anyway. – Cory Kramer Oct 07 '16 at 11:41
  • I am not a regular python coder,i am just helping a friend.Here's my code : I have edited my post,please check. – Von Oct 07 '16 at 12:11
  • Side note: [please do not **ever** store passwords in plain text](http://plaintextoffenders.com/about/) – Cory Kramer Oct 07 '16 at 12:14

1 Answers1

1

I would do this :

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)

Also refer to Calculating arithmetic mean (average) in Python for the mean value.

Community
  • 1
  • 1
C.LECLERC
  • 510
  • 3
  • 12
  • thanks for your solution.....but a minor change: maxval = str(max(numbers)) minval = str(min(numbers)) – Von Oct 07 '16 at 11:53