2012-10-18 93 views
2

我已經開始使用谷歌Python類,但我得到了一些奇怪的結果,一整天的調試並沒有幫助我解決它。Python - 返回無

似乎正在發生的事情是,函數返回的是None而不是我分配給它們的值,但是爲什麼發生這種情況正在讓我不知所措。我在一些調試代碼中寫道,並試圖通過它,但我看到是什麼導致了這種行爲。

下面是一些調試輸出的一個示例:

C:\Users\toshiba\Dropbox\DEV\python\google-python-exercises\basic>python string2.py 
front_back 
    X got: None expected: 'abxcdy' 
OK got: 'abcxydez' expected: 'abcxydez' 
OK got: 'KitDontenut' expected: 'KitDontenut' 

的代碼是從Google的類和由我寫然後功能。

# F. front_back 
# Consider dividing a string into two halves. 
# If the length is even, the front and back halves are the same length. 
# If the length is odd, we'll say that the extra char goes in the front half. 
# e.g. 'abcde', the front half is 'abc', the back half 'de'. 
# Given 2 strings, a and b, return a string of the form 
# a-front + b-front + a-back + b-back 
def front_back(a, b): 
    # +++your code here+++ 

    # Debug hardcode setting 
    # set to 1 to debug (default 0 off) 
    letsDebug = 0 

    alpha, bravo = a, b 
    if letsDebug == 1: 
     endString = a \ 
     + ' ' \ 
     + b 
     return endString 

    lenA = len(alpha) 
    lenB = len(bravo) 

    if lenA % 2 == 1: 
    statAlpha = 'odd' 
    else: 
    statAlpha = 'even' 

    if lenB % 2 == 1: 
    statBravo = 'odd' 
    else: 
    statBravo = 'even' 
    if letsDebug == 2: 
     endString = a \ 
     + ' ' \ 
     + b \ 
     + ' ' \ 
     + statAlpha \ 
     + ' ' \ 
     + statBravo 
     return endString 

    workB = lenB/2 
    workA = lenA/2 
    if letsDebug == 3: 
     endString = a \ 
     + ' ' \ 
     + b \ 
     + ' ' \ 
     + statAlpha \ 
     + ' ' \ 
     + statBravo \ 
     + ' ' \ 
     + str(workA) \ 
     + ' ' \ 
     + str(workB) 
     return endString 

    if statAlpha == 'even': 
    aFront, aBack = alpha[:workA], alpha[-workA:] 
    else: 
    aFront, aBack = alpha[:(workA+1)], alpha[-workA:] 

    if statBravo == 'even': 
    bFront, bBack = bravo[:workB], bravo[-workB:] 
    else: 
    bFront, bBack = bravo[:(workB+1)], bravo[-workB:] 

    if letsDebug == 4: 
     endString = a \ 
     + ' ' \ 
     + str(workA) \ 
     + ' ' \ 
     + b \ 
     + ' ' \ 
     + str(workB) \ 
     + ' ' \ 
     + statAlpha \ 
     + ' ' \ 
     + statBravo \ 
     + ' ' \ 
     + aFront \ 
     + ' ' \ 
     + bFront \ 
     + ' ' \ 
     + aBack \ 
     + ' ' \ 
     + bBack \ 
     + ' ' \ 
     + aFront + bFront + aBack + bBack 
    else: 
     endString = aFront + bFront + aBack + bBack 

    return endString 


# Simple provided test() function used in main() to print 
# what each function returns vs. what it's supposed to return. 
def test(got, expected): 
    if got == expected: 
    prefix = ' OK ' 
    else: 
    prefix = ' X ' 
    print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected)) 


# main() calls the above functions with interesting inputs, 
# using the above test() to check if the result is correct or not. 
def main(): 
    print 'verbing' 
    test(verbing('hail'), 'hailing') 
    test(verbing('swiming'), 'swimingly') 
    test(verbing('do'), 'do') 

    print 
    print 'not_bad' 
    test(not_bad('This movie is not so bad'), 'This movie is good') 
    test(not_bad('This dinner is not that bad!'), 'This dinner is good!') 
    test(not_bad('This tea is not hot'), 'This tea is not hot') 
    test(not_bad("It's bad yet not"), "It's bad yet not") 

    print 
    print 'front_back' 
    test(front_back('abcd', 'xy'), 'abxcdy') 
    test(front_back('abcde', 'xyz'), 'abcxydez') 
    test(front_back('Kitten', 'Donut'), 'KitDontenut') 

if __name__ == '__main__': 
    main() 

非常感謝任何誰可以破譯我在這裏去的地方。

+0

如果你沒有返回任何東西,或者你顯式返回'None',Python中的函數返回'None'。 –

+1

你應該只發布你想要幫助的代碼的相關部分。我這次編輯了不相關的部分,但是對於未來,請確保在發佈您的問題之前這樣做 – inspectorG4dget

+0

代碼在not_bad和front_back中均未通過測試。 你刪除了太多。這應該是兩個問題嗎? 或者最好是讀另一塊? – Patrick

回答

1

它看起來像在front_back末整個塊縮進一個層次太多。從if letsDebug == 4:return endString - 這是在它之上開始的else塊的所有部分(針對語句if statBravo == 'even':else)。我猜這應該是在功能範圍內。

+0

我回去重新整理了整個函數,它沒有改變代碼。我與空白的第一場戰鬥,它並不好玩。我認爲它一定是與剪切和粘貼混淆,並沒有顯示在jedit中。我會嘗試其他編輯,看看他們是否更好地處理它。我喜歡崇高的文字2,也許這將防止這種事情發生如此多的展望。試圖在一個有數千行模塊的模塊中跟蹤空間與標籤的想法是可怕的。 – Patrick

+0

我會建議使用IDE或文本編輯器,可以自動執行空格/製表符轉換和自動縮進。我個人使用Eclipse IDE與PyDev,但主要是因爲我也使用其他語言的Eclipse。它在粘貼文本時自動將空格轉換爲製表符(或其他方式),並將其縮進到您粘貼的塊的級別。 –

3

如果在front_back()中未包含return語句,那麼您有一條路徑不在最後。這一個:

if statBravo == 'even':