Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. def get_filename():
  2.     return input("Enter filename: ")
  3.  
  4. def open_file(filename):
  5.     try:
  6.         return open(filename, "r")
  7.        
  8.     except FileNotFoundError:
  9.         print("Filename", filename, "not found!")
  10.         return None
  11.  
  12. def names_and_countries(file_object):
  13.     new_dict = {}
  14.     for line in file_object:
  15.         name, country = line.split(" ") # Skilar lista með name og country
  16.         country = country.strip('\n')
  17.         if name not in new_dict:
  18.             new_dict[name] = {country}
  19.         else:
  20.             new_dict[name].add(country)
  21.     return new_dict
  22.  
  23. def print_names_and_countries(my_dict):
  24.     highest = ''
  25.     count = 0
  26.     for name in my_dict.items():
  27.         print('{}:'.format(name[0]))
  28.         for country in name[1]:
  29.             print("\t{}".format(country))
  30.         if len(name[1]) >  count: # LEngdin af settinu [1]
  31.             highest = name[0]
  32.             count = len(name[1])
  33.     print('{} has been to {} countries'.format(highest, count))
  34.  
  35.    
  36. def main():
  37.     file_object = open_file(get_filename())
  38.     x = names_and_countries(file_object)
  39.     print_names_and_countries(x)
  40.  
  41. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement