I am making a small address book program as practice, do you know what's wrong with my code when I tried to add new entry?
Also, how can I make it go back to choose 1-4 instead of keep in the same sub condition like error 2?
Thanks so much!
print('|-----Welcome to Q\'s Addrss Book-------|')
print('|--------------------------------------|')
print('|Please choice from the following:-----|')
print('|----------1: Find Contacts----------|')
print('|----------2: Add Contacts----------|')
print('|----------3: Delete Contacts----------|')
print('|----------4: Quit Address Book--------|')
i = int(input('Can I help you? :'))
address = {'ray':1234,'simon':2345,'alen':8888}
while 1:
if i == 1:
x=input('What\'s his/her name?')
print(address[x])
if i == 2:
x = (input('New Contact name?'))
if address[x] is not None:
z = str(input('Contact'+x+' with phone number: '+str(address[x])+ ' address already existed, do you want to override?(Yes/No)'))
if z == 'yes':
address[x] = input('New number?')
elif z == 'no':
break
else:
print('Please choose yes or no')
else:
address[x] = input('New number?')
if i == 3:
z = input('Who you want to delete:')
if address[z] is not None:
del address[z]
else:
print('Contact not existed!')
if i == 4:
break
Error1:
>>>
|-----Welcome to Q's Addrss Book-------|
|--------------------------------------|
|Please choice from the following:-----|
|----------1: Find Contacts----------|
|----------2: Add Contacts----------|
|----------3: Delete Contacts----------|
|----------4: Quit Address Book--------|
Can I help you?:2
New Contact name?q
Traceback (most recent call last):
File "/Users/xxx/Documents/1.py", line 18, in <module>
if address[x] is not None:
KeyError: 'q'
>>>
Error 2: keep looping in a sub if condition:
>>>
|-----Welcome to Q's Addrss Book-------|
|--------------------------------------|
|Please choice from the following:-----|
|----------1: Find Contacts----------|
|----------2: Add Contacts----------|
|----------3: Delete Contacts----------|
|----------4: Quit Address Book--------|
Can I help you?:1
What's his/her name?ray
1234
What's his/her name?
>>>
Ok, thanks for all your guys, I have made it work, here is the correct code:
print('|-----Welcome to Q\'s Addrss Book-------|')
print('|--------------------------------------|')
print('|Please choice from the following:-----|')
print('|----------1: Find Contacts----------|')
print('|----------2: Add Contacts----------|')
print('|----------3: Delete Contacts----------|')
print('|----------4: Quit Address Book--------|')
address = {'ray':123456789,'simon':222222222,'alen':88888888}
while 1:
i = int(input('Can I help you?'))
if i == 1:
x=input('What\'s his/her name?')
if x in address:
print(address[x])
else:
print('Contact does not exist!')
if i == 2:
x = (input('New Contact name?'))
if x in address:
z = str(input('Contact'+x+' with phone number: '+str(address[x])+ ' address already existed, do you want to override?(Yes/No)'))
if z == 'yes':
address[x] = input('New number?')
elif z == 'no':
continue
else:
print('Please choose yes or no')
else:
address[x] = input('New number?')
if i == 3:
z = input('Who you want to delete:')
if z in address:
del address[z]
else:
print('Contact does not exist!')
if i == 4:
break