I made a fizz-buzz-like algorithm with python, so I will post it.
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.
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