在for/else块中打印错误错误触发

2024-05-15 21:58:53 发布

您现在位置:Python中文网/ 问答频道 /正文

test_dict = [{"account_number": 12345, "Name" : "Nick", "Last_Name" : "Davis"},
             {"account_number": 76531, "Name" : "Carl", "Last_Name" : "Maison"},
             {"account_number": 75321, "Name" : "Mary", "Last_Name" : "Depp"},
             {"account_number": 12345, "Name" : "Gary", "Last_Name" : "Davis"},
             {"account_number": 45896, "Name" : "Jessica", "Last_Name" : "Johnson"},
             {"account_number": 12345, "Name" : "Kathy", "Last_Name" : "Davis"}
]

acc = input("Insert Account Number: ")

result_list = []

for i in test_dict :
    try:
        if int(acc) == i["account_number"]:
            result_list.append(i)
        elif len(acc) != 5:
            print(f"There are 5 digits in the Account Number, you inserted {len(acc)}. Try again.")
            break
    except:
        print("There are no letters in Account Number. Please Insert 5 digits.")
        break
else:
    print(f"Sorry, Could not find Account Number {acc}. Try again.")

我需要在test_dict中找到指定帐号的所有名字和姓氏。除了最后一次检查外,我的所有错误检查都有效:当我插入正确的帐号时,它会告诉我“对不起,找不到帐号{acc}。请重试”。即使result_list中填充了信息

图像:

当帐号有效时,如何避免执行最后一个print语句

我不能在if语句之后使用break,因为它只会追加第一次出现的帐号。我需要所有的


Tags: nameintestnumberaccountresultdictlist
3条回答

在尝试在test_dict中查找匹配值之前,应该检查acc上的错误。一旦你确定它是有效的,你就可以用它来搜索。例如(使用列表理解生成result_list):

acc = input("Insert Account Number: ")
result_list = []
try:
    accnum = int(acc)
    if len(acc) != 5:
        print(f"There are 5 digits in the Account Number, you inserted {len(acc)}. Try again.")
    else:
        result_list = [a for a in test_dict if a["account_number"] == accnum]
        if len(result_list) == 0:
            print(f"Sorry, Could not find Account Number {acc}. Try again.")
except:
    print("There are no letters in Account Number. Please Insert 5 digits.")

print(result_list)

输出(用于12345的输入):

[
 {'account_number': 12345, 'Name': 'Nick', 'Last_Name': 'Davis'},
 {'account_number': 12345, 'Name': 'Gary', 'Last_Name': 'Davis'},
 {'account_number': 12345, 'Name': 'Kathy', 'Last_Name': 'Davis'}
]

只有使用break语句退出循环时,else:块才会触发。但是正如您所说的,您不能使用break语句,因为它会提前退出。因此,请尝试检查结果列表是否为空

for i in test_dict :
    ...

if not result_list:
    print(f"Sorry, Could not find Account Number {acc}. Try again.")

我还会将其他错误检查移出循环。如果用户输入错误,甚至不需要启动循环。提前检查一下,如果他们输入了错误的号码,就早点停下来。或者再问他们一次,这是你的偏好

while True:
    acc = input("Insert Account Number: ")
    if len(acc) != 5:
        print(f"There are 5 digits in the Account Number, you inserted {len(acc)}. Try again.")
        continue
    try:
        acc = int(acc)
        break
    except:
        print("There are no letters in Account Number. Please Insert 5 digits.")
        continue

result_list = []

for i in test_dict :
    if acc == i["account_number"]:
        result_list.append(i)

if not result_list:
    print(f"Sorry, Could not find Account Number {acc}. Try again.")

这将为将循环转换为列表打开大门。任何时候你发现自己在循环和调用append(),你就有可能成为列表理解的候选人

while True:
    # read `acc` as above

result_list = [i for i in test_dict if i["account_number"] == acc]

if not result_list:
    print(f"Sorry, Could not find Account Number {acc}. Try again.")

根据提供的示例,我能够使所有条件正确工作

代码如下:


test_dict = [{"account_number": 12345, "Name" : "Nick", "Last_Name" : "Davis"},
             {"account_number": 76531, "Name" : "Carl", "Last_Name" : "Maison"},
             {"account_number": 75321, "Name" : "Mary", "Last_Name" : "Depp"},
             {"account_number": 12345, "Name" : "Gary", "Last_Name" : "Davis"},
             {"account_number": 45896, "Name" : "Jessica", "Last_Name" : "Johnson"},
             {"account_number": 12345, "Name" : "Kathy", "Last_Name" : "Davis"}
]

acc = input("Insert Account Number: ")

result_list = []

for i in test_dict :
    try:
        if int(acc) == i["account_number"]:
            result_list.append(i)
        elif len(acc) != 5:
            print(f"There are 5 digits in the Account Number, you inserted {len(acc)}. Try again.")
            break
    except:
        print("There are no letters in Account Number. Please Insert 5 digits.")
        break
        
else:
    if len(result_list) == 0:
        print(f"Sorry, Could not find Account Number {acc}. Try again.")

相关问题 更多 >