Review of the basics of Python (FizzBuzz)

Introduction

I made a fizz-buzz-like algorithm with python, so I will post it.

Premise

When it is a multiple of 2, ~ is an even number When it is a multiple of 3, ~ is a multiple of 3 When it is a multiple of 6, ~ is a multiple of 6 Otherwise ~ is any other number Is displayed.

You can enter the number of judgments first.

Run

First, create a function to determine the multiple.

def sample(x):
  if (x % 3 == 0 and x % 2 ==0):
    print(x,"Is a multiple of 6")
  elif (x % 3 == 0):
    print(x,"Is a multiple of 3")
  elif (x % 2 == 0):
    print(x,"Is even")
  else:
    print(x,"Is any other number")

A : is added to the end of the ʻif or ʻelse line, ʻelif, not elsif, Note that we are using ʻand instead of &&, Other than that, it's not much different from ruby.

Next, be sure to enter the number of impressions.

print("How many do you want to display?")
y = int(input())

If it's just input, y = input () seems to be fine, but it wasn't judged as a number, so it's written like this.

Finally, make a multiple judgment for the number of times you enter.


for x in range(1, y + 1):
  sample(x)

The number of executions is determined by range (1, y + 1). Here, the process is to perform sample (x) for all integers from 1 to y entered earlier. sample (x) is to call the function defined first.

The whole code is as follows.

# coding:utf-8
import sys

def sample(x):
  if (x % 3 == 0 and x % 2 ==0):
    print(x,"Is a multiple of 6")
  elif (x % 3 == 0):
    print(x,"Is a multiple of 3")
  elif (x % 2 == 0):
    print(x,"Is even")
  else:
    print(x,"Is any other number")

print("How many do you want to display?")
y = int(input())

for x in range(1, y + 1):
  sample(x)

Recommended Posts

Review of the basics of Python (FizzBuzz)
Basics of Python ①
Basics of python ①
About the basics list of Python basics
Learn the basics of Python ① Beginners
[Python3] Understand the basics of Beautiful Soup
I didn't know the basics of Python
In search of the fastest FizzBuzz in Python
[Python3] Understand the basics of file operations
Basics of Python scraping basics
the zen of Python
# 4 [python] Basics of functions
Basics of python: Output
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
Towards the retirement of Python2
About the ease of Python
python: Basics of using scikit-learn ①
About the features of Python
Basics of Python × GIS (Part 1)
The Power of Pandas: Python
How much do you know the basics of Python?
Basics of Python x GIS (Part 3)
Python basics ⑤
Paiza Python Primer 5: Basics of Dictionaries
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Python basics ④
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Getting Started with Python Basics of Python
Change the Python version of Homebrew
Python basics ③
Python basics
[Python] Understanding the potential_field_planning of Python Robotics
Basics of Python x GIS (Part 2)
Python basics
Python basics
Python basics ③
What beginners learned from the basics of variables in python
Python basics ②
Python basics ②
[Super basics of Python] I learned the basics of the basics, so I summarized it briefly.
Review the basics in 1 minute! Python Priority queue for fast minimums
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Change the length of Python csv strings
Check the behavior of destructor in Python
Learn the basics of Theano once again
[Learning memo] Basics of class by python
Learn the basics while touching python Variables
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
Review the concept and terminology of regression
[Python] Understand the content of error messages
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda