Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_filename():
- return input("Enter filename: ")
- def open_file(filename):
- try:
- return open(filename, "r")
- except FileNotFoundError:
- print("Filename", filename, "not found!")
- return None
- def names_and_countries(file_object):
- new_dict = {}
- for line in file_object:
- name, country = line.split(" ") # Skilar lista með name og country
- country = country.strip('\n')
- if name not in new_dict:
- new_dict[name] = {country}
- else:
- new_dict[name].add(country)
- return new_dict
- def print_names_and_countries(my_dict):
- highest = ''
- count = 0
- for name in my_dict.items():
- print('{}:'.format(name[0]))
- for country in name[1]:
- print("\t{}".format(country))
- if len(name[1]) > count: # LEngdin af settinu [1]
- highest = name[0]
- count = len(name[1])
- print('{} has been to {} countries'.format(highest, count))
- def main():
- file_object = open_file(get_filename())
- x = names_and_countries(file_object)
- print_names_and_countries(x)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement