Python在通讯录程序中的If与字典用法
我正在做一个小的通讯录程序作为练习,你知道我在尝试添加新条目时,代码哪里出错了吗?
另外,我该如何让程序在选择1到4之间返回,而不是像错误2那样一直停留在同一个子条件里?
非常感谢大家的帮助!
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
错误1:
>>>
|-----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'
>>>
错误2:在一个子条件中一直循环:
>>>
|-----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?
>>>
好的,谢谢大家,我已经让它正常工作了,这里是正确的代码:
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
1 个回答
0
‘Stack of Pancakes’已经回答了你问题的第二部分(见上面的评论)
当你想检查一个联系人是否已经存在时,需要明确的是,如果x
不是字典中的一个键,address[x] == None
永远不会返回True
。如果Python在字典中找不到某个键,它不会返回None
,而是会抛出一个KeyError
异常。Python在处理异常方面比一些其他语言要宽松得多——这篇文章对此有更详细的介绍,你也可以阅读官方文档
处理这些异常的‘pythonic’方式更像这样:
if x not in address:
....
然后在你删除联系人的代码块中:
try:
del address[z]
except KeyError:
print('Contact does not exist!')
这里的代码“尝试”从字典中删除这个键。如果这个键一开始就不存在,代码的第二行会抛出一个KeyError
异常。但是我们有一个except
语句专门用来捕捉KeyError
异常,所以你不会看到错误堆栈信息。执行会转到最后一行的打印函数。