0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】if文を使わないFizzBuzz

Last updated at Posted at 2025-03-01
def fizz():
    while True:
        yield ""
        yield ""
        yield "Fizz"


def buzz():
    while True:
        yield ""
        yield ""
        yield ""
        yield ""
        yield "Buzz"


if __name__ == "__main__":
    limit = 100

    for i, Fizz, Buzz in zip(range(limit), fizz(), buzz()):
        print((Fizz + Buzz) or i + 1)

実行結果

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...
Fizz
97
98
Fizz
Buzz

Appendix

kazuhoさんのFizzBuzz実装にインスピレーションを受けて。
Pythonならジェネレーターで似たようなことができるの良い仕様だと思う。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?