I'm tryiing to make contact list. There is error at bottom at this code and also try isn't work I'm don't know much about class. I want remove errors(that can cause by user) I want the shortest and easiest way without import module(but I would like to use bulit in module). I'm using pycharm and python 3.9.
help = '''1: help
2: add
3: delete
4: print book
5: modify
6: read file
7: print file
8: exit'''
line = '--------------------'
class Contact:
book = {}
def __init__(self):
print(help)
print(line)
def add(self):
try:
print('add')
name = input('input name')
number = input('input number')
self.book[name] = number
except FileNotFoundError:
print('Error: Unknown Error')
def delete(self):
try:
print('delete')
name = input('input name')
del self.book[name]
except:
print('Error: KeyError or Unknown Error')
def printbook(self):
try:
print('printbook')
print('there is ' + str(len(self.book)) + ' people')
for x in self.book:
print('name: ' + x, end='')
print('\tnumber: ' + self.book[x])
except:
print('Error: Unknown Error')
def modify(self):
try:
print('modify')
name = input('input name')
new_name = input('input new name')
number = input('input number')
self.book[name] = number
self.book[new_name] = self.book.pop(name)
except:
print('Error: KeyError or Unknown Error')
def readfile(self):
try:
file_name = input('input file name to read') + '.txt'
with open(file_name, "r") as file:
book_list = file.readlines()
for x in book_list:
x = x.split()
self.book[x[0]] = x[1]
print('read ' + file)
except:
print('Error: FileError, FileContents or Unknown Error')
def printfile(self):
try:
file_name = input('input file name to write') + '.txt'
with open(file_name, "w") as file:
key = list(self.book.keys())
val = list(self.book.values())
for x in range(len(self.book)):
file.write(key[x] + ' ' + val[x] + '\n')
print('print at ' + file)
except:
print('Error: FileError or Unknown Error')
def program_help():
try:
print(help)
detale = int(input('which one?'))
if detale == 1:
print('it gives you help')
elif detale == 2:
print('it makes you able to add person')
elif detale == 3:
print('it allows you delete person')
elif detale == 4:
print('it prints book so you can able to see')
elif detale == 5:
print('it allows you to edit name&number')
elif detale == 6:
print('it makes you able to add contact number in your file. You should not add \'.txt\'')
elif detale == 7:
print('it makes you able to print contact number to file. You should not add \'.txt\'')
elif detale == 8:
print('it finishes program')
detale = input('need more detale? (y/n)')
if detale == 'y':
program_help()
except:
print('Error: Unknown Error')
b = Contact()
while True:
answer = int(input('choose function'))
if answer == 1:
print(help)
while detale != 'y' or 'Y' or 'n' or 'N':
detale = input('need detale? (y/n)')
if detale != 'y' or 'Y' or 'n' or 'N':
print('Error: please type again')
if detale == 'y':
program_help()
elif answer == 2:
b.add()
elif answer == 3:
b.delete()
elif answer == 4:
b.printbook()
elif answer == 5:
b.modify()
elif answer == 6:
b.readfile()
elif answer == 7:
b.printfile()
elif answer == 8:
print('exit')
quit()
print(line)
else:#error
print('Error: User typed an unknown word')
Aucun commentaire:
Enregistrer un commentaire