python初学者:在异常之前尝试两件事?
我正在尝试编写一个脚本,它接受数字输入,然后检查以查看 (a) 输入实际上是一个数字,并且 (b) 该数字小于或等于17 我尝试过各种各样的“如果”语句,但都没有用,现在我正试图用“尝试”语句来表达我的想法。这是我迄今为止最好的尝试:python初学者:在异常之前尝试两件事?,python,combinations,try-except,Python,Combinations,Try Except,我正在尝试编写一个脚本,它接受数字输入,然后检查以查看 (a) 输入实际上是一个数字,并且 (b) 该数字小于或等于17 我尝试过各种各样的“如果”语句,但都没有用,现在我正试图用“尝试”语句来表达我的想法。这是我迄今为止最好的尝试: def listlength(): print "How many things (up to 17) do you want in the list?" global listlong listlong = raw_input(">
def listlength():
print "How many things (up to 17) do you want in the list?"
global listlong
listlong = raw_input("> ")
try:
listlong = int(listlong)
listlong <= 17
except:
print "Gotta be a number less than 17, chumpy!"
listlength()
liststretcher()
def listlength():
打印“您希望列表中有多少项(最多17项)”
全球上市公司
listlong=原始输入(“>”)
尝试:
listlong=int(listlong)
listlong你得到了大部分答案:
def isIntLessThanSeventeen(listlong):
try:
listlong = int(listlong) # throws exception if not an int
if listlong >= 17:
raise ValueError
return True
except:
return False
print isIntLessThanSeventeen(16) # True
print isIntLessThanSeventeen("abc") # False
您需要使用if语句来检查关系,如果合适,手动引发异常。要修复您的解决方案:
def listlength():
print "How many things (up to 17) do you want in the list?"
global listlong
listlong = raw_input("> ")
try:
listlong = int(listlong)
listlong <= 17
except:
print "Gotta be a number less than 17, chumpy!"
listlength()
return
liststretcher()
这样函数就不会调用自身,而对liststretcher的调用只会在输入有效时发生。您缺少的,以及S.Lott试图引导您的,是语句listlong没有理由避免递归,这也可以起作用:
length = ""
while not length.isdigit() or int(length) > 17:
length = raw_input("Enter the length (max 17): ")
length = int(length)
def prompt_list_length(err=None):
if err:
print "ERROR: %s" % err
print "How many things (up to 17) do you want in the list?"
listlong = raw_input("> ")
try:
# See if the list can be converted to an integer,
# Python will raise an excepton of type 'ValueError'
# if it can't be converted.
listlong = int(listlong)
except ValueError:
# Couldn't be converted to an integer.
# Call the function recursively, include error message.
listlong = prompt_list_length("The value provided wasn't an integer")
except:
# Catch any exception that isn't a ValueError... shouldn't hit this.
# By simply telling it to 'raise', we're telling it to not handle
# the exception and pass it along.
raise
if listlong > 17:
# Again call it recursively.
listlong = prompt_list_length("Gotta be a number less than 17, chumpy!")
return listlong
input = prompt_list_length()
print "Final input value was: %d" % input
,而是要具体说明要捕获哪些异常。您做了什么listlong
def listlength():
print "How many things (up to 17) do you want in the list?"
global listlong
listlong = raw_input("> ")
value = None
while value is None or and value > 17:
try:
listlong = int(listlong)
except:
print "Gotta be a number less than 17, chumpy!"
value = None
listlong = value
liststretcher()
length = ""
while not length.isdigit() or int(length) > 17:
length = raw_input("Enter the length (max 17): ")
length = int(length)
def prompt_list_length(err=None):
if err:
print "ERROR: %s" % err
print "How many things (up to 17) do you want in the list?"
listlong = raw_input("> ")
try:
# See if the list can be converted to an integer,
# Python will raise an excepton of type 'ValueError'
# if it can't be converted.
listlong = int(listlong)
except ValueError:
# Couldn't be converted to an integer.
# Call the function recursively, include error message.
listlong = prompt_list_length("The value provided wasn't an integer")
except:
# Catch any exception that isn't a ValueError... shouldn't hit this.
# By simply telling it to 'raise', we're telling it to not handle
# the exception and pass it along.
raise
if listlong > 17:
# Again call it recursively.
listlong = prompt_list_length("Gotta be a number less than 17, chumpy!")
return listlong
input = prompt_list_length()
print "Final input value was: %d" % input