I wrote the following program to study Python, I think there is a more efficient way to write, so I made this article so that I would like to hear the opinions of experts. If you are an expert who says that you can write in shorter lines, please comment.
Japanese calendar conversion
#Years(Year)Enter with
year=int(input())
Syear=[1868, 1912, 1926, 1989, 2019, 9999]
wareki=['Meiji', 'Taisho', 'Showa', 'Heisei', 'Reiwa', '']
i=0
#Japanese calendar conversion
while Syear[i]<=year:
Wyear=year-Syear[i]+1
Wname=wareki[i]
i+=1
#output
print(str(year)+'Year is'+Wname+str(Wyear)+'Year')
Age judgment
import datetime
#Birthday(Year)Enter
Tymd=input()
#Divided by date
param=Tymd.split('/')
Tyear=int(param[0])
Tmonth=int(param[1])
Tday=int(param[2])
#Get the current date
Today=datetime.datetime.now()
year=Today.year
month=Today.month
day=Today.day
#Age calculation
age=int(year)-int(Tyear)
if int(month) < int(Tmonth):
age-=1
else:
if month==Tmonth:
if day < Tday:
age-=1
#output
print(Tymd+'People'+str(age)+'age')
Day of the week judgment
import math
#Input value(Example: 1997/9/11)To receive
x=input()
#Divide the input value into year, month and day
y=x.split('/')
year = int(y[0])
month=int(y[1])
day=int(y[2])
#output
print(str(year)+'Year'+str(month)+'Month'+str(day)+'day', end='')
if month < 3:
month=month+12
year=year-1
#Ask for the day of the week
weekday = (year + math.floor(year / 4) - math.floor(year / 100) + math.floor(year / 400) + math.floor((13*month+8) / 5) + day) % 7
week = ['Day', 'Month', 'fire', 'water', 'wood', 'Money','soil']
#output
print(str(week[weekday])+'It's the day of the week')
Multi-digit digit decomposition
#Receive input value
x=int(input())
num=[]
#Decompose every digit
while x>0:
num.append(x%10)
x//=10
num.reverse()
#output
for i in range(len(num)):
print(num[i], end=' ')
Leap year judgment
#Years(Year)Enter
year=int(input())
#Leap year judgment
if year%4==0:
if year%100==0:
if year%400==0:
print(str(year)+'The year is a leap year')
else:
print(str(year)+'The year is a leap year')
else:
print(str(year)+'The year is not a leap year')
Zodiac judgment
#Years(Year)Enter
year=int(input())
#All zodiac signs
Alleto =["Monkey(Monkey)", "Rooster(Bird)", "Dog(Dog)", "亥(Wild boar)", "Child(mouse)"
, "Cow(Ushi)", "Tiger(Tiger)", "Rabbit(Rabbits)", "Dragon(Tatsu)", "Snake(Snake)", "Horse(Horse)", "Not yet(sheep)"]
#Zodiac judgment&output
print(str(year)+'The zodiac of the year'+Alleto[year%12])
Primality test
#Enter a number greater than or equal to 2
num=int(input())
# 0:Prime number
# 1:Not a prime number
sosu=0
#Primality test
for i in range(2, num):
if num%i==0:
sosu=1
break
#output
if sosu==0:
print(str(num)+'Is a prime number')
elif sosu==1:
print(str(num)+'Is not a prime number')
Recommended Posts