Challenge Fizz Buzz! Problems with Python in 5 patterns (only now up to 398)

I personally tackled the problem of Fizz Buzz! At Python-only Mokumokukai, so here is the result report!

Challenge the Fizz Buzz problem

With numbers from 1 to 398 If it is divisible by 3, "Fizz!" Is displayed. If it is divisible by 5, "Buzz!" Is displayed. If it is divisible by 3 and 5, "Fizz Buzz!" Will be displayed. In cases other than the above, the numbers are displayed as they are. Tip: Combine for and if statements

An example that simply uses only for and if

sample1.py


for x in range(398):
    x = x + 1
    if x % 3 == 0:
        if x % 5 == 0:
            fb = "Fizz Buzz!"
        if x % 5 != 0:
            fb = "Fizz!"
    if x % 3 != 0:
        if x % 5 == 0:
            fb = "Buzz!"
        if x % 5 != 0:
            fb = x
    print(fb)

An example of using something like and, or, or elif

sample2.py


for x in range(398):
    x = x + 1
    if x % 3 == 0 and x % 5 == 0:
        fb = "Fizz Buzz!"
    elif x % 3 == 0:
        fb = "Fizz!"
    elif x % 5 == 0:
        fb = "Buzz!"
    else:
        fb = x
    print(fb)

Example of using a function

sample3.py


def fb(x):
    if x % 3 == 0 and x % 5 == 0:
        fb = "Fizz Buzz!"
    elif x % 3 == 0:
        fb = "Fizz!"
    elif x % 5 == 0:
        fb = "Buzz!"
    else:
        fb = x
    print(fb)

for x in range(398):
    x = x + 1
    fb(x)

Example using recursive call

sample4.py


def count(x):
    if x > 1:
        count(x - 1)
    fb(x)

def fb(x):
    if x % 15 == 0:
        fb = "Fizz Buzz!"
    elif x % 3 == 0:
        fb = "Fizz!"
    elif x % 5 == 0:
        fb = "Buzz!"
    else:
        fb = x
    print(fb)
        
count(398)

Example of using a class

sample5.py


class FizzBuzz:
    def __init__(self, x):
        self.count(x)

    def count(self, x):
        if x > 1:
            self.count(x - 1)
        self.fb(x)

    def fb(self, x):
        if x % 15 == 0:
            fb = "Fizz Buzz!"
        elif x % 3 == 0:
            fb = "Fizz!"
        elif x % 5 == 0:
            fb = "Buzz!"
        else:
            fb = x
        print(fb)

FizzBuzz(398)

Output result

1
2
Fizz!
4
Buzz!
Fizz!
7
8
Fizz!
Buzz!
11
Fizz!
13
14
Fizz Buzz!
16
17
Fizz!
19
Buzz!
Fizz!
22
23
Fizz!
Buzz!
26
Fizz!
28
29
Fizz Buzz!
31
32
Fizz!
34
Buzz!
Fizz!
37
38
Fizz!
Buzz!
41
Fizz!
43
44
Fizz Buzz!
46
47
Fizz!
49
Buzz!
Fizz!

...
...
...

386
Fizz!
388
389
Fizz Buzz!
391
392
Fizz!
394
Buzz!
Fizz!
397
398

Problem source

I happened to get a theme, so I did it, but I think the problem with the URL below is the cause. https://qiita.com/Sekky0905/items/7e2b13f2a001384c7fc4

Recommended Posts

Challenge Fizz Buzz! Problems with Python in 5 patterns (only now up to 398)
Fizz Buzz in Python
Fizz Buzz in Python
[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python
Solve Fizz Buzz (equivalent to paiza rank C) in Python
Let's try Fizz Buzz in Python
I wrote Fizz Buzz in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Try logging in to qiita with Python
Python to remember only with hello, worlds
How to work with BigQuery in Python
To work with timestamp stations in Python
[REAPER] How to play with Reascript in Python
Convert PDFs to images in bulk with Python
3 steps to put Python + mecab in yum only
Log in to Yahoo Business with Selenium Python
How to use tkinter with python in pyenv
20th Offline Real-time How to Write Problems in Python
How to convert / restore a string with [] in python
Try to solve the programming challenge book with python3
How to do hash calculation with salt in Python
Explain in detail how to make sounds with python
How to run tests in bulk with Python unittest
Convert the image in .zip to PDF with Python
Super Primer to python-Getting started with python3.5 in 3 minutes
I was addicted to scraping with Selenium (+ Python) in 2020
I want to work with a robot in python.
How to run setUp only once in python unittest
Multi-digit multiplication time up to 300 million digits in python
How to read csv containing only integers in Python
Combining problems in Python
Behavioral Patterns in Python
Structural Patterns in Python
Creational Patterns in Python
Design and test Verilog in Python only with Veriloggen and cocotb.
[Road to intermediate Python] Install packages in bulk with pip
Wrap C/C ++ with SWIG to speed up Python processing. [Overview]
How to import Python library set up in EFS to Lambda
How to extract any appointment in Google Calendar with Python
[Python] Localize sound source only to human voice with ReSpeaker
Challenge problem 5 with Python: lambda ... I decided to copy without
Try to bring up a subwindow with PyQt5 and Python
Things to keep in mind when using Python with AtCoder
13th Offline Real-time How to Solve Writing Problems in Python
Things to keep in mind when using cgi with python.
How to log in to AtCoder with Python and submit automatically
How to write offline real-time Solving E05 problems with Python
Connect to BigQuery with Python
Scraping with selenium in Python
To flush stdout in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Login to website in Python
Connect to Wikipedia with Python
Post to slack with Python 3
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Design Patterns in Python: Introduction