1

我正在尝试编写一个接受数字输入的脚本,然后检查以查看

(a) 输入实际上是一个数字,并且 (b) 所讨论的数字小于或等于 17。

我尝试了各种“if”语句都无济于事,现在我正试图围绕“try”语句。这是我迄今为止最好的尝试:

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 函数。但是第二个元素(<=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() 命令之前使用相同定义中的语句?

作为对 S.Lott 的回应,如下:我的意图是“try:listlong <=17”将检查“listlong”变量是否小于或等于 17;如果该检查失败,它将移至“除外”;如果它通过了,它将转到下面的 liststretcher()。

阅读迄今为止的答案,我有大约八件事要跟进......

4

6 回答 6

2

你有大部分的答案:

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
于 2012-02-28T17:58:02.510 回答
1

您缺少的以及 S.Lott 试图引导您的是,该语句listlong <= 17不会引发异常。它只是一个产生 True 或 False 的条件表达式,然后您会忽略该值。

你的意思可能是assert( listlong <= 17 ),如果它的条件为 False,它会抛出一个 AssertionError 异常。

于 2012-02-28T18:36:23.133 回答
1

您将需要使用 if 语句来检查关系,并在适当时手动引发异常。

于 2012-02-28T17:58:47.490 回答
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 。

于 2012-02-28T18:01:42.700 回答
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
于 2012-02-29T03:28:21.457 回答
0
length = ""
while not length.isdigit() or int(length) > 17:
   length = raw_input("Enter the length (max 17): ")
length = int(length)
于 2012-02-28T18:27:36.907 回答