Python:运行一个没有效果的语句来检查是否抛出异常?

Python:运行一个没有效果的语句来检查是否抛出异常?,python,exception,exception-handling,pycharm,Python,Exception,Exception Handling,Pycharm,如果用户输入无效,我希望发生一些事情(查看异常块): 请注意我是如何运行table\u dictionary[name.title()]的,即使它没有任何作用。如果用户输入不在字典中,我会这样做,它会告诉用户这是无效的输入。PyCharm突出显示了它,并告诉我,语句似乎没有任何效果。我只是想知道这是不是一个好的做法 编辑:注意,如果用户也为事实提供了无效输入,我还想打印“错误:无效输入”。我的示例包括这一点。在table\u字典中检查键可能是一种更好的做法 name = raw_input("\

如果用户输入无效,我希望发生一些事情(查看异常块):

请注意我是如何运行
table\u dictionary[name.title()]
的,即使它没有任何作用。如果用户输入不在字典中,我会这样做,它会告诉用户这是无效的输入。PyCharm突出显示了它,并告诉我,
语句似乎没有任何效果
。我只是想知道这是不是一个好的做法


编辑:注意,如果用户也为事实提供了无效输入,我还想打印“错误:无效输入”。我的示例包括这一点。

在table\u字典中检查键可能是一种更好的做法

name = raw_input("\nEnter someone's name (x to exit): ")

if name == "x":
    print 'Closing'
    break
elif name.title() not in table_dictionary:
    print "Error: Invalid input"
else:  
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    print name.title() + "'s " + fact + " is " + str(table_dictionary[name.title()][fact.title()])
为了更好的可读性,可以将.title()直接移动到输入

name = raw_input("\nEnter someone's name (x to exit): ").title()

if name == "X":
    print 'Closing'
    break
elif name not in table_dictionary:
    print "Error: Invalid input"
else:  
    fact = raw_input("What do you want to know about " + name + "?: ")
    print name + "'s " + fact + " is " + str(table_dictionary[name][fact.title()])

检查table_字典中的键可能是更好的做法

name = raw_input("\nEnter someone's name (x to exit): ")

if name == "x":
    print 'Closing'
    break
elif name.title() not in table_dictionary:
    print "Error: Invalid input"
else:  
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    print name.title() + "'s " + fact + " is " + str(table_dictionary[name.title()][fact.title()])
为了更好的可读性,可以将.title()直接移动到输入

name = raw_input("\nEnter someone's name (x to exit): ").title()

if name == "X":
    print 'Closing'
    break
elif name not in table_dictionary:
    print "Error: Invalid input"
else:  
    fact = raw_input("What do you want to know about " + name + "?: ")
    print name + "'s " + fact + " is " + str(table_dictionary[name][fact.title()])

运行一些代码只是为了查看是否引发异常是可以的,但是通常有更好的方法来测试某件事情是否可能。在您的特定情况下,我建议使用
In
操作符,使用
if
elif
else
测试键是否在字典中,以解决问题:

name = raw_input("\nEnter someone's name (x to exit): ")
if name.title() in table_dictionary:
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    print name.title() + "'s " + fact + " is " + str(table_dictionary[name.title()][fact.title()])
elif name == "x":
    print 'Closing'
    break
else:
    print "Error: Invalid input"

运行一些代码只是为了查看是否引发异常是可以的,但是通常有更好的方法来测试某件事情是否可能。在您的特定情况下,我建议使用
In
操作符,使用
if
elif
else
测试键是否在字典中,以解决问题:

name = raw_input("\nEnter someone's name (x to exit): ")
if name.title() in table_dictionary:
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    print name.title() + "'s " + fact + " is " + str(table_dictionary[name.title()][fact.title()])
elif name == "x":
    print 'Closing'
    break
else:
    print "Error: Invalid input"

我觉得这更优雅,更像蟒蛇:

name = raw_input("\nEnter someone's name (x to exit): ")
if name == "x":
    print 'Closing'
    break
try:
    fact_dictionary = table_dictionary[name.title()]
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    print name.title() + "'s " + fact + " is " + str(fact_dictionary[fact.title()])
except KeyError:
    print "Error: Invalid input"
这样,我们可以捕获无效输入的两个可能异常,如果
name==“x”

它还使您的代码更具可读性,并防止PyCharm抱怨


我不同意其他一些评论者的意见,他们建议检查字典中的键等等,你使用异常进行流控制是正确的,这是Python的方式。

我认为这更优雅、更具Python风格:

name = raw_input("\nEnter someone's name (x to exit): ")
if name == "x":
    print 'Closing'
    break
try:
    fact_dictionary = table_dictionary[name.title()]
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    print name.title() + "'s " + fact + " is " + str(fact_dictionary[fact.title()])
except KeyError:
    print "Error: Invalid input"
这样,我们可以捕获无效输入的两个可能异常,如果
name==“x”

它还使您的代码更具可读性,并防止PyCharm抱怨


我不同意其他一些评论者的意见,他们建议检查字典中的键等等,你使用异常进行流控制是正确的,这是Python的方式。

我建议这样做。现在没有什么是无用的

try:
    name = raw_input("\nEnter someone's name (x to exit): ")
    assert name != 'x', "Closing"
    assert table_dictionary.has_key(name.title()), "Error: Invalid input '%s'" % name
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    assert table_dictionary[name.title()].has_key(fact.title()), "Error: Invalid input '%s'" % fact
    print name.title() + "'s " + fact + " is " + str(table_dictionary[name.title()][fact.title()])
except AssertionError as err:
    print err

我建议你这样做。现在没有什么是无用的

try:
    name = raw_input("\nEnter someone's name (x to exit): ")
    assert name != 'x', "Closing"
    assert table_dictionary.has_key(name.title()), "Error: Invalid input '%s'" % name
    fact = raw_input("What do you want to know about " + name.title() + "?: ")
    assert table_dictionary[name.title()].has_key(fact.title()), "Error: Invalid input '%s'" % fact
    print name.title() + "'s " + fact + " is " + str(table_dictionary[name.title()][fact.title()])
except AssertionError as err:
    print err
你能行

\uu=table\u dictionary[name.title()
因为这是python的惯例,使用下划线来表示没有使用这个变量。否则pycharm会抱怨未使用的变量

你能行

\uu=table\u dictionary[name.title()

因为这是python的惯例,使用下划线来表示没有使用这个变量。否则pycharm会抱怨未使用的变量

我应该澄清一下,如果用户也给出了错误的事实,我想运行“Error:Invalid input”。您的示例不会这样做。我应该澄清一下,如果用户也给出了错误的事实,我想运行“Error:Invalid input”。您的示例不会这样做。您可以编写
断言表\u字典。has\u key(name.title())
您不需要
==True
。我非常喜欢在这一行中使用assert的方法,因为我认为它更加明确,并且允许将
AssertionError
KeyError
分开处理。但不要忘记,在查找
事实
@rbricheno时,您还必须捕获
打印
行上发生的潜在
关键错误
。谢谢您指出这一点。我将根据您的建议编辑我的答案。您只需编写
assert table\u字典。has\u key(name.title())
您不需要
==True
。我非常喜欢在这一行中使用assert的方法,因为我认为它更加明确,并且允许将
AssertionError
KeyError
分开处理。但不要忘记,在查找
事实
@rbricheno时,您还必须捕获
打印
行上发生的潜在
关键错误
。谢谢您指出这一点。我会根据你的建议修改我的答案。