我正在尝试编写一个接受数字输入的脚本,然后检查以查看
(a)输入实际上是一个数字,和 (b)有关数目少于或等于17。
我尝试了各种各样的“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()
liststretcher()
它适用于try中的第一个元素:如果它不是数字,我必须再次运行listlength函数。但是完全忽略了第二个元素(&lt; = 17)。
我也试过
try:
listlong = int(listlong) and listlong <= 17
...但是这仍然只给我一个功能性的第一次检查,并完全忽略了第二次。
如果我有两个try语句,我也会得到相同的结果:
try:
listlong = int(listlong)
except:
print "Gotta be a number, chumpy!"
listlength()
try:
listlong <=17
except:
print "Gotta be less than 17!"
listlength()
liststretcher()
有没有办法尝试:检查两件事,并且在移动超过异常之前要求两者都通过?或者,在继续执行liststretcher()命令之前,我是否必须在同一个定义中创建两个不同的try:语句?
回应S.Lott,下面:我的意图是“try:listlong&lt; = 17”将检查“listlong”变量是否小于或等于17;如果检查失败,则会转移到“除外”;如果它通过,它将转移到下面的liststretcher()。
阅读迄今为止的答案,我有八件事需要跟进...
答案 0 :(得分:1)
你有大部分答案:
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
答案 1 :(得分:1)
您需要使用if语句检查关系,并在适当时手动引发异常。
答案 2 :(得分:1)
你所缺少的,以及S.Lott试图引导你的是,listlong <= 17
语句不会引发异常。它只是一个条件表达式,产生True或False,然后忽略该值。
您的意思可能是assert( listlong <= 17 )
,如果条件为False则抛出AssertionError异常。
答案 3 :(得分:0)
好吧,修复你的解决方案:
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()
问题是你在不需要时使用递归,所以试试这个:
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()
这样,该函数不会调用自身,只有在输入有效时才会调用 liststretcher 。
答案 4 :(得分:0)
length = ""
while not length.isdigit() or int(length) > 17:
length = raw_input("Enter the length (max 17): ")
length = int(length)
答案 5 :(得分:0)
没有理由避免递归,这也有效:
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