412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        nums = []
        for i in range(1,n+1):
            if (i%3 != 0 and i%5 !=0):
                nums.append(str(i))
            elif i%15 == 0:
                nums.append('FizzBuzz')
            elif i%3 == 0:
                nums.append('Fizz')
            else:
                nums.append('Buzz')
        return nums

没啥说的,注意i不能打出来就好,str(就可以了)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 你过的不好 我会等 你过的好 我不打扰
    0826_初阅读 1,358评论 0 0
  • 阿里想做社交,一直是大家众所周知的事儿,从几乎销声匿迹的“来往”到支付宝 9.0 版强行加入的社交属性。但直到最近...
    浪里个男阅读 4,417评论 0 5
  • 斜阳无意向晚,谁堪想,日影断。墨染经年梦未全,书携流景心欲远。题诗赋万卷。 四分之一春秋,只换来日安候。烁星春夜倦...
    四月未及阅读 2,773评论 0 0
  • 5年前的辞职报告,缅怀一下过去的日子 尊敬的郎组织: 我志愿离开今麦郎,不再接受郎的领导,不再听从郎的指挥…… 悄...
    大唐逸民阅读 1,001评论 0 2