LoginSignup
0
1

More than 3 years have passed since last update.

pythonを使った年齢計算

Last updated at Posted at 2019-12-15
import datetime

def age_now():
    now_time = str(datetime.date.today())
    split_time = now_time.split("-")
    print("生年月日を入力してください")
    b_year = int(input("年:"))
    b_month = int(input("月:"))
    b_day = int(input("日:"))

    age = int(((int(split_time[0]) * 10000 + int(split_time[1]) * 100 + int(split_time[2])) - (b_year * 10000 + b_month * 100 + b_day)) / 10000)
    print("現在の年齢は{}才です".format(age))

age_now()

簡単にこんな感じです。int関数使いすぎててよくわからなくなってますね。気が向いたら減らしてみます。

追記

import datetime

def age_now():
    time = str(datetime.date.today())
    now_time = int("".join(time.split("-")))
    print("生年月日を入力してください")
    birthday = int(input())
    age = int(((now_time - birthday) / 10000))
    print("現在の年齢は{}才です".format(age))

age_now()

こんな感じですね。入力自体はさっきの奴のほうがやりやすかったですかね。まぁ今回はこんなところで・・・

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1