Python Fizzbuzz problems with loop

2020-04-21 05:32发布

I've searched for the answer for about an hour, and it seems most people have coded fizzbuzz a different way than myself.

However, having tried everything to figure out why this simple code will not work I'm getting extremely frustrated.

Can anyone point out the simple problem I'm sure I'm having? The code runs but it just returns the value 1.

def fizzbuzz(intList):
    for n in intList:
        if n % 3 == 0 and n % 5 == 0:
            return n.replace(str(n),"Fizzbuzz")
        elif n % 3 == 0:
            return n.replace(str(n),"Fizz")
        elif n % 5 == 0:
            return n.replace(str(n),"Buzz")
        else:
            return n

7条回答
聊天终结者
2楼-- · 2020-04-21 06:01

how a python function should look like if we want to see the next result in the interactive mode of the python interpreter:

>>> fizz(15)
[ 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz' ]
查看更多
登录 后发表回答