Python如果&地址簿程序中的字典用法

时间:2014-08-09 20:18:07

标签: python

我正在制作一个小型地址簿程序作为练习,当我尝试添加新条目时,您知道我的代码有什么问题吗?

另外,如何让它返回选择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

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'
>>> 

错误2:在子if条件下继续循环:

>>> 
|-----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 :(得分:0)

'Stack of Pancakes'已经回答了你问题的第二部分(见上面的评论)

当您尝试检查联系人是否已存在时,请务必澄清address[x] == None如果True不是字典中的关键字,则永远不会返回x。如果Python无法在字典中找到密钥,则它不会返回None,但会抛出KeyError异常。 Python使用异常远比其他语言更自由 - this article更详细地介绍它们,您还可以阅读the docs

与他们合作的'pythonic'方式更像是这样:

if x not in address:
    ....

然后在您的块中删除联系人:

try:
    del address[z]
except KeyError:
    print('Contact does not exist!')

这里代码'尝试'从字典中删除密钥。如果密钥从未存在,则在此代码的第二行引发KeyError异常。但是我们有一个专门用于捕获except异常的KeyError子句,因此您不会看到堆栈跟踪。执行移动到最后一行的打印功能。