简单的电话簿查找 else 函数 运行 即使 if 语句为真
Simple phonebook lookup else function running even when if statement is true
我正在尝试制作一本简单的 phone 书,如果您输入
1:您将联系人添加到字典中,如果
2 您根据输入的名称(键)查找字典,如果
3 你根据输入的数字(value)查字典
当我 运行 根据值(输入 3)进行键查找时,它 returns else 函数 'this is invalid' 不管它是否为真。
有人能破译吗?
#Input contact name
if button == 1:
name = input('Please enter the contact name:')
if name in contacts:
print("The name you entered already exists in the address book --> %s:%s"\
%(name,contacts[name]))
flag = input("Whether to modify user information (YES/NO):")
if flag== 'YES':
tel = input('Please enter the users contact phone number:')
contacts.update({name:tel}) #Update dictionary
print("Contacts have been updated!")
else:
continue
else:
contacts[name] = input('Please enter the contact phone number:')
print("Contact has been saved!")
#Search by contact name
if button == 2:
name = input('Please enter the contact name:')
if name in contacts:
print("%s : %s "%(name,contacts[name]))
else:
print('The name you entered is no longer in the address book! ')
#Search by contact number
if button == 3:
numba = input('Please enter the contact number:')
lookup = []
for key,value in contacts.items():
if(value == numba):
lookup.append(key)
print('Name(s) matching number is',lookup)
else:
print('This is invalid')
试试这个:
if button == 3:
numba = input('Please enter the contact number:')
lookup = []
for key,value in contacts.items():
if(value == numba):
lookup.append(key)
if lookup: # True if len(lookup) != 0
print('Name(s) matching number is', lookup)
else:
print('This is invalid')
可能有点牵强,但我是这样工作的:
numba = input('Please enter the contact number: ')
lookup = []
for key,value in contacts.items():
if(numba == key):
lookup.append(value)
print('Name(s) matching number is', lookup)
if(int(numba) > len(contacts.items())):
print('This is invalid')
我正在尝试制作一本简单的 phone 书,如果您输入 1:您将联系人添加到字典中,如果 2 您根据输入的名称(键)查找字典,如果 3 你根据输入的数字(value)查字典
当我 运行 根据值(输入 3)进行键查找时,它 returns else 函数 'this is invalid' 不管它是否为真。
有人能破译吗?
#Input contact name
if button == 1:
name = input('Please enter the contact name:')
if name in contacts:
print("The name you entered already exists in the address book --> %s:%s"\
%(name,contacts[name]))
flag = input("Whether to modify user information (YES/NO):")
if flag== 'YES':
tel = input('Please enter the users contact phone number:')
contacts.update({name:tel}) #Update dictionary
print("Contacts have been updated!")
else:
continue
else:
contacts[name] = input('Please enter the contact phone number:')
print("Contact has been saved!")
#Search by contact name
if button == 2:
name = input('Please enter the contact name:')
if name in contacts:
print("%s : %s "%(name,contacts[name]))
else:
print('The name you entered is no longer in the address book! ')
#Search by contact number
if button == 3:
numba = input('Please enter the contact number:')
lookup = []
for key,value in contacts.items():
if(value == numba):
lookup.append(key)
print('Name(s) matching number is',lookup)
else:
print('This is invalid')
试试这个:
if button == 3:
numba = input('Please enter the contact number:')
lookup = []
for key,value in contacts.items():
if(value == numba):
lookup.append(key)
if lookup: # True if len(lookup) != 0
print('Name(s) matching number is', lookup)
else:
print('This is invalid')
可能有点牵强,但我是这样工作的:
numba = input('Please enter the contact number: ')
lookup = []
for key,value in contacts.items():
if(numba == key):
lookup.append(value)
print('Name(s) matching number is', lookup)
if(int(numba) > len(contacts.items())):
print('This is invalid')