program that asks for the name and date (year, month and day) of a person's birth in Python

0

Hello, I'm having trouble with the following code in Python :.

The idea is to create a program that asks for the name and date (year, month and day) of birth of a person.

The program should also request the current date (year, month and day).

Based on these dates, you should determine the person's age.

Outcome result:

What's your name? Joaquim

Born in what year? 2001

What month was born? 2

Born on what day? 23

What year are we? 2018

What month are we? 4

On what day? 9

Joaquim is 16 years old

The problem is whether a person turns years on April 5, 1974 and the current date is January 2, 2018, that person should be 43!

And it's here that you're always giving me 44 years! I do not know if I understood myself! ??!

Here is my code

    
asked by anonymous 11.04.2018 / 14:44

1 answer

0

Okay, you can create two date objects (dataNasc and currentTime) and subtract to get the timedelta (difference between them) and after that consider the average days of a year, getting more or less like this.

import datetime

#Entrada de valores
print('='*18)
print('Exercício 2: idade')
print('='*18)
nome = input ("Como se chama? ")
ano = eval (input ("Nasceu em que ano? "))
mes = eval (input ("Nasceu em que mês? "))
dia = eval (input ("Nasceu em que dia? "))
ano_atual = eval (input ("Ano atual? "))
mes_atual = eval (input ("Mês atual? "))
dia_atual = eval (input ("Dia atual? "))
dataNasc = datetime.date(ano, mes, dia)
dataAtual = datetime.date(ano_atual, mes_atual, dia_atual)

#diferença retorna em timedelta
diferenca = (dataAtual - dataNasc)
#Cálculos e Resultados
rslt = (diferenca.days / 365.25)
#ano_atual-ano

if (dia == dia_atual and mes == mes_atual):
    print ("O %s tem %d anos!" %(nome, rslt))
else:
    ((dia > dia_atual and mes == mes_atual) or mes < mes_atual)
    print ("O %s tem %d anos!" %(nome, rslt))

For more questions about comparisons with dates and manipulations access here

    
11.04.2018 / 14:57