Python practice Convert the input year to the Japanese calendar

Purpose

Created as a Python exercise. Converts the Western calendar entered as an integer value to the Japanese calendar.

specification

What you can do

The response will be like this.

Please enter the year.
2020
The year 2020 is the second year of Reiwa.

What to use

Variables, assignments, if elif else statements, four rules operations, character input, comparison operations, and logical operations are used.

code

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='')

Construction

Input part of the Christian era

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)

Processing when the entered year is the Meiji era

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.

Processing when it was the first 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.

Output when it was the first year

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 specifysep` in the blank, it will be output without spaces.

Normal output (if not the first year)

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.

In the case of Reiwa

python


#Reiwa
elif(int_input >=2019):

Since the final year is not fixed, there is only one condition.

Exception handling

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.

the end

That's it.

I created it as an example using the basic description. Thank you for reading.

Information that I was allowed to refer to

Qiita Python basic course (8 branches)

note.nkmk.me Print strings, numbers and variable values with Python's print function

Recommended Posts

Python practice Convert the input year to the Japanese calendar
Method to convert between Western calendar and Japanese calendar
Convert the image in .zip to PDF with Python
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
[Python] How to use input ()
Convert python 3.x code to python 2.x
Program to determine leap year from the Christian era [Python]
Determine the date and time format in Python and convert to Unixtime
Leave the troublesome processing to Python
Practice! !! Introduction to Python (Type Hints)
Convert markdown to PDF in Python
Convert some Japanese names to antonyms
Add Python 2.7 Japanese documentation to Dash.app
How to judge that the cross key is input in Python3
Workflow to convert formula (image) to python
In the python command python points to python3.8
How to get the Python version
Convert list to DataFrame with python
After calling the Shell file on Python, convert CSV to Parquet.
Python> list> Convert double list to single list
[Python] How to import the library
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Convert the result of python optparse to dict and utilize it
Program to convert Japanese to station name
How to handle Japanese in Python
Python> tuple> Convert double tuple to single tuple
[Python] Change the alphabet to numbers
I want to batch convert the result of "string" .split () in Python
[Python] How to convert db file to csv
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
How to set the server time to Japanese time
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Convert Excel data to JSON with python
Python3 standard input I tried to summarize
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
[Introduction to Udemy Python3 + Application] 41. Input function
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
How to display python Japanese with lolipop
Python amateurs try to summarize the list ①
Introducing Japanese input system to Serene Linux
Convert Python date types to RFC822 format
How to enter Japanese with Python curses
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
[Python] Add comments to standard input files
The road to compiling to Python 3 with Thrift
Sample to convert image to Wavelet with Python