Created as a Python exercise. Converts the Western calendar entered as an integer value to the Japanese calendar.
The response will be like this.
Please enter the year.
2020
The year 2020 is the second year of Reiwa.
Variables, assignments, if elif else statements, four rules operations, character input, comparison operations, and logical operations are used.
nengo.py
int_input = int(input("Please enter the year.\n"))
#Meiji
if(int_input >= 1868 and int_input <=1911):
str_nengo = "Meiji"
int_year = int_input - 1867
if(int_year == 1):
int_lastYear = int_input - 1864
str_lastYear = "Keio"
print("Year", int_input , "Year is", str_nengo, "First year (", str_lastYear, int_lastYear ,"Year).\n",sep='')
else:
print("Year", int_input , "Year is", str_nengo, int_year, "It's the year.\n",sep='')
#Taisho
elif(int_input >=1912 and int_input <=1925):
str_nengo = "Taisho"
int_year = int_input - 1911
if(int_year == 1):
int_lastYear = int_input - 1867
str_lastYear = "Meiji"
print("Year", int_input , "Year is", str_nengo, "First year (", str_lastYear, int_lastYear ,"Year).\n",sep='')
else:
print("Year", int_input , "Year is", str_nengo, int_year, "It's the year.\n",sep='')
#Showa
elif(int_input >=1926 and int_input <=1988):
str_nengo = "Showa"
int_year = int_input - 1925
if(int_year == 1):
int_lastYear = int_input - 1911
str_lastYear = "Taisho"
print("Year", int_input , "Year is", str_nengo, "First year (", str_lastYear, int_lastYear ,"Year).\n",sep='')
else:
print("Year", int_input , "Year is", str_nengo, int_year, "It's the year.\n",sep='')
#Heisei
elif(int_input >=1989 and int_input <=2018):
str_nengo = "Heisei"
int_year = int_input - 1988
if(int_year == 1):
int_lastYear = int_input - 1925
str_lastYear = "Showa"
print("Year", int_input , "Year is", str_nengo, "First year (", str_lastYear, int_lastYear ,"Year).\n",sep='')
else:
print("Year", int_input , "Year is", str_nengo, int_year, "It's the year.\n",sep='')
#Reiwa
elif(int_input >=2019):
str_nengo = "Reiwa"
int_year = int_input - 2018
if(int_year == 1):
int_lastYear = int_input - 1988
str_lastYear = "Heisei"
print("Year", int_input , "Year is", str_nengo, "First year (", str_lastYear, int_lastYear ,"Year).\n",sep='')
else:
print("Year", int_input , "Year is", str_nengo, int_year, "It's the year.\n",sep='')
#Exception handling
else:
# if(int_input < 1868):
print("Year", int_input , "The year is quite old.\n",sep='')
python
int_input = int(input("Please enter the year.\n"))
ʻInt_input A variable that stores the entered year. Since it is an integer, it is ʻint_
.
ʻInput ()Character input ʻInt (input ())
Restricts input as an integer
\ n
Line break at the end (no need to break)
python
#Meiji
if(int_input >= 1868 and int_input <=1911):
str_nengo = "Meiji"
int_year = int_input - 1867
ʻIf (): if statement. ʻInt_input> = 1868
Condition 1. If the entered year is after 1868.
ʻInt_input <= 1911 Condition 2. If the entered year is before 1911. ʻAnd
logical operator. Meiji when both conditions are met.
A variable that stores the str_nengo
year. Since it is a character string, it is called str_
.
ʻInt_year A variable that stores the year of the Japanese calendar. ʻInt_input --1867
Subtract 1867 from the entered year to get the year of the Meiji era.
python
if(int_year == 1):
int_lastYear = int_input - 1864
str_lastYear = "Keio"
If it was one year, it is also the last year of the previous era.
1868 AD is the first year of the Meiji era (4th year of Keio). I want to write
.
So I added another condition. Create a nested if statement inside the first if statement.
ʻIf (int_year == 1): If the Japanese calendar year ʻint_year
is 1.
ʻInt_lastYearThe last year of the previous era. Here, we are looking for the final year (4) of Keio.
str_lastYear` The previous era.
Now you are ready to output.
python
print("Year", int_input , "Year is", str_nengo, "First year (", str_lastYear, int_lastYear ,"Year).\n",sep='')
print ()
A function that prints. Multiple elements are concatenated and output using ,
.
" AD "
This is a character string. When printing a character string, enclose it in quotation marks.
ʻInt_inputThis is a variable. If you want to print the variable, leave it as it is.
" First year (", str_lastYear, int_lastYear," year) The previous era and last year.
\ nLine break.
sep =''Separate specification. By default, there is a space between the elements so it looks a little longer. If you specify
sep` in the blank, it will be output without spaces.
python
print("Year", int_input , "Year is", str_nengo, int_year, "It's the year.\n",sep='')
Only the processing of the "first year" part is deleted.
This completes the Meiji era. Next is Taisho, Showa, Heisei, and Reiwa.
Basically, it has the same structure as the Meiji era,
Meiji is ʻif sentence, Taisho, Showa, Heisei, and Reiwa are ʻelif
.
For ʻif, you can set the second and subsequent conditions with ʻelif
.
python
#Reiwa
elif(int_input >=2019):
Since the final year is not fixed, there is only one condition.
python
else:
# if(int_input < 1868):
print("Year", int_input , "The year is quite old.\n",sep='')
ʻElse is used as opposed to the previous ʻif
and ʻelif. ʻElse
is a process other than the conditions described so far.
Specifically, this is the case when the Christian era is older than 1868.
This time, we are targeting Meiji, Taisho, Showa, Heisei, and Reiwa. In terms of numbers, it is output that the target is after 1868, and before that it is a fairly old age.
That's it.
I created it as an example using the basic description. Thank you for reading.
Qiita Python basic course (8 branches)
note.nkmk.me Print strings, numbers and variable values with Python's print function
Recommended Posts