Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- IMPORTS
- '''
- import io
- import os.path
- import sys
- import ast
- '''
- GLOBAL VARS
- '''
- #Using a list of dictionaries so I can addapt it to use more info other than name and number
- contact_list = [];
- '''
- FUNCTIONS
- '''
- def LoadContact():
- if not os.path.isfile("contacts.txt"): #If doesn't exist then return
- return
- fich = open("contacts.txt","r")
- for line in fich:
- contact_list.append(ast.literal_eval((line[:-1] if line[-1] == "\n" else line)))
- fich.close();
- def Menu():
- os.system("cls")
- print('''\
- MENU...
- 1- Look for a contact.
- 2- Insert a contact.
- 3- List contacts by letter.
- 0- Leave.
- ''')
- return input("Option: ")
- def SearchContact(Name):
- Name = Name.upper()
- for i in contact_list:
- if i['Name'].upper() == Name:
- return i
- return 'NULL'
- def SearchLetter(l):
- l = l.upper()
- print('Name Number')
- for i in contact_list:
- if i['Name'][0].upper() == l[0]:
- print("{} {}".format(i['Name'],i['Number']))
- def Save():
- fich = open("contacts.txt","w")
- for i in contact_list:
- n = i['Name']
- num = i['Number']
- fich.write("{")
- fich.write("'Name':'{}','Number':'{}'".format(n,num))
- fich.write("}\n")
- fich.close();
- '''
- MAIN
- '''
- LoadContact();
- while True:
- opc=int(Menu())
- if opc == 0:
- Save()
- break
- elif opc == 1:
- con = SearchContact(input("\nWrite a name to look for: "))
- if con == 'NULL':
- print('Name not found.')
- else:
- print("Name: {}\nNumber: {}".format(con['Name'],con['Number']))
- os.system("pause")
- elif opc == 2:
- n = {'Name':input('Type the name: '),'Number':input('Type the number: ')}
- contact_list.append(n)
- print('Contact ({}, {}) successfully inserted.'.format(n['Name'], n['Number']))
- os.system("pause")
- elif opc == 3:
- SearchLetter(input('Type the leter you want to look for: '))
- os.system("pause")
- else:
- print('Wrong option: Use only from 0 to 3.')
- os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement