小编Mas*_*son的帖子

异常后停止功能

我在创建错误处理方法时遇到了一些问题.遇到错误后,sub继续,好像什么也没发生.这就是我所拥有的:

try 
{
    int numericID = Convert.ToInt32(titleID);
}
catch(Exception)
{
    errorHandling("Invalid Title");
}

void errorHandling(string error)
{
    MessageBox.Show("You have encountered an error: " + error, "Error");
    return;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c#

1
推荐指数
1
解决办法
6172
查看次数

检查字典是否包含列表中的至少一个特定值

检查“目标”列表中的字典中是否存在至少一个特定值的最优雅方法是什么?我过去一直在使用 or 语句,但这些可能相当长:

d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}

if 'six' in d.values() or 'eight' in d.values() or 'ten' in d.values() or 'fifteen' in d.values():
    # do something
else:
    # do something else
Run Code Online (Sandbox Code Playgroud)

创建一个目标列表,然后遍历该列表以分别根据字典检查每个目标是否更好?在这种情况下,我还需要在 if/else 块的末尾放置中断以确保它不会被多次触发:

targets = ['six', 'eight', 'ten', 'fifteen']
d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}

for t in targets:
    if t in d.values():
        # do something
        break  # to ensure the thing isn't triggered multiple …
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x

1
推荐指数
1
解决办法
39
查看次数

标签 统计

c# ×1

dictionary ×1

python ×1

python-3.x ×1