Question

in python Write a program that reads the date of birth from the user as 3...

in python
Write a program that reads the date of birth from the user as 3 integer values (day, month, year)
and then computes and prints their exact age in days. You need to take into account the leap You
may not use any built-in functions related to date and time!
Example:
Someone born on 01/01/2000 is 365 days * 18 years + 5 days (since the years 2000, 2004, 2008, 2012, and 2016 are all leap years) + 352 days (passed in 2019) = 6,927 days old!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

Please comment in case of a doubt.

Also, take care of indentation by referring to code screenshots.

Code:

import datetime

# function to check for a leap year
def isLeap(year):
  
if(year%400 == 0):
return True
elif(year%100 == 0):
return False
elif(year%4 == 0):
return True
else:
return False

# get today's date
tmp = datetime.date.today()
# store it in an array of 3 elements
now = [tmp.day, tmp.month, tmp.year]

# birthday input from user
birthday = [0] * 3
birthday[0] = int(input('Enter day of birth: '))
birthday[1] = int(input('Enter month of birth: '))
birthday[2] = int(input('Enter year of birth: '))

# days in each month
days_in_each_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# total days
days = 0

# loop for days
if(birthday[0] > now[0]):
now[0] = now[0] + days_in_each_month[birthday[1]]
now[1] = now[1] - 1

# calculate days
days = days + now[0] - birthday[0]

# for months
if(birthday[1] > now[1]):
now[2] = now[2] - 1
  
# add days till 12 month of the birthday year
# and then days of months of the present year
tmp = birthday[1] + 1
while(tmp <= 12):
days = days + days_in_each_month[tmp]
tmp = tmp + 1
  
tmp = 1
while(tmp <= now[1]):
days = days + days_in_each_month[tmp]
tmp = tmp + 1
  
else: # normally add days for each month

tmp = birthday[1] + 1
  
# loop for months
while(tmp <= now[1]):
days = days + days_in_each_month[tmp]
tmp = tmp + 1
  
# run loop for years
tmp = birthday[2] + 1
while(tmp < now[2]):
days = days + 365
tmp = tmp + 1
  
# for leap years
# birth year
if(isLeap(birthday[2]) and birthday[1] <= 2):
days = days + 1
# for next year
for i in range(birthday[2] + 1, now[2]):
if(isLeap(i)):
days =days + 1
# current year
if(isLeap(now[2]) and now[1] > 2):
days = days + 1
  
print(days, 'days old!')

Sample run:

Code screenshots:

Add a comment
Know the answer?
Add Answer to:
in python Write a program that reads the date of birth from the user as 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3. Date Printer Write a program that reads a string from the user containing a...

    Python 3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018 3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • 3. Date Printer Write a program that reads string from the user containing a date in...

    3. Date Printer Write a program that reads string from the user containing a date in the form mm/dd/yyyy. It should print the date in the form March 12, 2014 . ( it is starting out with python third edition ) Can anyone please do it by python program .

  • Task 1 Write a Python program that reads in any number of birth years until the...

    Task 1 Write a Python program that reads in any number of birth years until the number zero is entered. The program will then print out the average age and how old the youngest and oldest, respectively. For this task, you can start from the pseudo-code (separately) you received to get started. Example of program execution (user entries in numbers (year)): Enter year of birth. To finish, enter the number 0. Year: 1998 Year: 1932 Year: 1887 Error: Unreasonable year....

  • Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2:...

    Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2: Leap Years (2 Points) Your task is to write a program that takes in both a month and year (defined in the definitions file) and outputs the number of days for that month. It should take into account whether the year is a leap year or not. The resulting number of days in a given month/year combo is to be assigned to the variable...

  • Write in python code Project 11-1: Birthday Calculator Create a program that accepts a name and...

    Write in python code Project 11-1: Birthday Calculator Create a program that accepts a name and a birth date and displays the person's birthday the current day, the person's age, and the number of days until the person's next birthday Console Birthday Calculator Enter name: Joel Enter birthday (MM/DD/YY): 2/4/68 Birthday: Sunday, February 04, 1968 Today: Joel is 48 years old. Joel's birthday is in 73 days Tuesday, November 22, 2016 Continue? (y/n): y Enter name: Django Enter birthday (MM/DD/YY):...

  • Write a program that reads a string from the user containing a date in the form...

    Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018   I am asking for help in basic python please

  • Write a program called RentalRate.java which reads in a date of birth, today’s date and a...

    Write a program called RentalRate.java which reads in a date of birth, today’s date and a male/female designation from the user, and will then determine the basic daily and weekly rates for rental of an economy class car. Rental rate classes are: Best rate (male drivers, age 33–65 and female drivers, age 30-62)--$40.00per day, $200.00 per week Risk rate 1 (female drivers, age 25–29)–Best rate plus $10.00 per day or best rate plus $55.00 per week. Risk rate 2 (male...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT