[英]Python - returning None
我已經開始使用google 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'
該代碼來自googles類,然后是我編寫的函數。
# 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()
非常感謝任何能破譯我去這里的人的人。
如果在front_back()中,則返回的路徑不在最后一條語句之內,而該路徑未被return語句覆蓋。 這個:
if statBravo == 'even':
看起來front_back
末尾的整個塊縮進了太多級別。 從if letsDebug == 4:
return endString
這是在其上方開始的else塊的所有部分( if statBravo == 'even':
的語句的else
)。 我猜這應該在功能范圍內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.