2012-02-28 59 views
1

我想寫接受輸入數字的腳本,然後檢查python初學者:在異常之前嘗試兩件事情?

(一)該輸入實際上是一個數字, (二)有問題的數量較少大於或等於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() 

它適用於在嘗試的第一個元素:如果它不是一個數字,我必須通過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()命令之前,我必須在同一個定義中創建兩個不同的try:語句嗎?

對S.Lott的迴應如下:我的意圖是「嘗試:listlong < = 17」將檢查「listlong」變量是否小於或等於17;如果檢查失敗,則會移至「除外」;如果它通過,它會轉到下面的liststretcher()。

閱讀的答案至今,我已經得到了有關八件事跟進......

+0

[不使用'不同的是:'(http://docs.python.org/howto/doanddont.html#except) ,而是要具體說明你想要捕捉哪些例外。 – delnan 2012-02-28 17:59:44

+0

'listlong <= 17'你做了什麼?請**更新**問題以解釋您認爲該聲明的作用。請具體說明**該聲明應該做什麼。 – 2012-02-28 18:02:47

回答

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

您將需要使用if語句檢查關係,如果適用,手動引發異常。

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的呼叫僅在輸入有效時纔會發生。

0
length = "" 
while not length.isdigit() or int(length) > 17: 
    length = raw_input("Enter the length (max 17): ") 
length = int(length) 
1

你缺少什麼,以及美國洛特試圖帶領你,是該語句listlong <= 17不會引發異常。這只是一個條件表達式,可以產生True或False,然後忽略這個值。

你的意思是說,可能是assert(listlong <= 17),如果條件爲False,則拋出一個AssertionError異常。

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