2015-04-06 29 views
0
def test_countdown(self): 
    from_3 = '3\n2\n1\nBlastoff!' 
    self.assertEqual(countdown(3), from_3) 
    from_0 = 'Blastoff!' 
    self.assertEqual(countdown(0), from_0) 
#THIS IS THE TEST 
def countdown(n): 
    while n>0: 
     print(n) 
     n=n-1 
    print("Blastoff!") 
#This is my code for the function 

它不是通過測試,因爲它是走出來的「無」>倒計時後端(3)而不是「3 \ N2 \ N1 \ nBlastoff!」倒計時功能測試蟒蛇工作

+0

你似乎有混淆返回值和從打印輸出。打印將值打印到標準輸出。這與函數的返回值完全不同。您需要實際返回某些內容才能使用assertEqual。 – Lalaland

回答

0

你打印,不串聯:

def countdown(n): 
    return '\n'.join(map(str, range(n, 0, -1))) + '\nBlastoff!' 

或者像Lalaland以下建議:

def countdown(n): 
    result = '' 

    while n > 0: 
     result += str(n) + '\n' 
     n = n - 1 # decrement 

    result = result + '\nBlastoff!' 
    return result # not printing 
+0

您可能想考慮鏡像原始代碼的樣式。 https://gist.github.com/Lalaland/107457008470f700d1d4 – Lalaland

+0

對一個簡單的計數器使用while循環並不是一個好主意。 –

+0

我同意,但清晰度有時很重要。 – Lalaland