倒计时函数适用于测试蟒蛇


def test_countdown(self):
    from_3 = '3n2n1nBlastoff!'
    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(,而不是"321Blastoff!

您正在打印,而不是连接:

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

或者正如拉拉兰在下面建议的那样:

def countdown(n):
    result = ''
    while n > 0:
        result += str(n) + 'n'
        n = n - 1 # decrement
    result = result + 'nBlastoff!'
    return result # not printing 

最新更新