468,134 Members | 1,214 Online

# Calculating future date from input obtained date

3 2Bits
Hi all,

I am a newby in Python and after a bit of bragging to my wife, she challenged me to generate a due date calculator to confirm the due date of our first-born.

So, via
int(input("Date of conception: (enter in YYYYMMDD) "))
I obtain a date like 20201114.

But now I want to calculate the anticipated due date by taking that date and adding 280 days.

I am getting stuck in this, and would appreciate any help!

Thanks!!

Jorrit
Mar 12 '21 #1
3 2415
JorritTjeertes
3 2Bits
Hi all,

after several hours trying and failing I managed in the end. I am sure the code isn't too elegant, but it gave the result!

Happy to hear any suggestions or thoughts!

Jorrit

Expand|Select|Wrap|Line Numbers
1. print("Due date calculator")
2.
3. pregnancy_type = int(input("""
4. What type of pregnancy did you have?
5. 1: Natural
6. 2: 3-day IVF transfer
7. 3: 5-day IVF transfer
8.
9. Please choose 1, 2 or 3: """))
10.
11. print("Please enter the starting date of conception")
12. conception_day = int(input("Day (DD): "))
13. conception_month = int(input("Month (MM): "))
14. conception_year = int(input("Year (YYYY): "))
15.
16. #Calculating the date based on the inputs!!
17. import datetime
18. start_date = datetime.datetime(conception_year, conception_month, conception_day)
19.
20. # Last period:  280 days
21. # 3 day IVF:  263 days
22. # 5 day IVF:  261 days
23.
24. if pregnancy_type == 1:
25.     pregnancy_length_1 = 280
26.     future_date_1 = start_date + datetime.timedelta(days=pregnancy_length_1)
27.     print(f"""
28. Your due date is approximately: {future_date_1}
29.
30. Congratulations on your pregnancy!""")
31. elif pregnancy_type == 2:
32.     pregnancy_length_2 = 263
33.     future_date_2 = start_date + datetime.timedelta(days=pregnancy_length_2)
34.     print(f"""
35. Your due date is approximately: {future_date_2}
36.
37. Congratulations on your pregnancy!""")
38. elif pregnancy_type == 3:
39.     pregnancy_length_3 = 261
40.     future_date_3 = start_date + datetime.timedelta(days=pregnancy_length_3)
41.     print(f"""
42. Your due date is approximately: {future_date_3}
43.
44. Congratulations on your pregnancy!""")
45. else:
46.     print("Please only choose 1, 2 or 3")
47.
4 Weeks Ago #2
Niheel
2,418 Expert Mod 2GB
Congratulations to the soon to be parents!

Just ran the program. It works.

The only other thing you can do is add validation of the input and display errors. Maybe use a function to check input?

Possibly by using something like this:
Expand|Select|Wrap|Line Numbers
1. def check_user_input(input):
2.     try:
3.         # Convert it into integer
4.         val = int(input)
5.         print("Day is ", val)
6.     except ValueError:
8.
10.     conception_day = input("Day (DD):")
11.     check_user_input(conception_day)
12.
13. conception_day = ask_day()
14.
4 Weeks Ago #3
JorritTjeertes
3 2Bits
Hi Niheel,

Thanks for the feedback and suggestion!

I also exchanged the code:
{future_date_3}

with:
{future_date_3.strftime('%Y-%m-%d')}

to only show the date and not the time.

Thanks again!

Jorrit
4 Weeks Ago #4