Advertisement
themaleem

Untitled

Oct 18th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. from datetime import datetime, timedelta
  2.  
  3. def calculate_dates(last_period_date, period_length, cycle_length):
  4.     # Calculate the next period date
  5.     next_period_date = last_period_date + timedelta(days=cycle_length)
  6.  
  7.     # Period end date
  8.     period_end_date = last_period_date + timedelta(days=period_length)
  9.  
  10.     # The fertile window starts 3 to 5 days after the period ends and lasts for about 6 days.
  11.     # We'll calculate the start and end dates of this ovulation period.
  12.     ovulation_start_date = period_end_date + timedelta(days=3)  # Start of the fertile window
  13.     ovulation_end_date = period_end_date + timedelta(days=5) + timedelta(days=6)  # End of the fertile window
  14.  
  15.     return next_period_date, ovulation_start_date, ovulation_end_date
  16.  
  17. def get_date_input(prompt):
  18.     while True:
  19.         date_str = input(prompt)
  20.         try:
  21.             # Try to convert the input string to a datetime object
  22.             return datetime.strptime(date_str, "%Y-%m-%d")
  23.         except ValueError:
  24.             # If input is not a valid date, prompt the user to enter a new value
  25.             print("Invalid date format. Please enter the date in YYYY-MM-DD format.")
  26.  
  27. def get_integer_input(prompt):
  28.     while True:
  29.         try:
  30.             # Try to convert the input to an integer
  31.             return int(input(prompt))
  32.         except ValueError:
  33.             # If input is not an integer, prompt the user to enter a new value
  34.             print("Invalid input. Please enter a valid number.")
  35.  
  36. # Get user inputs
  37. print("Please enter the following information:")
  38. last_period_date = get_date_input("Last period start date (YYYY-MM-DD): ")
  39. period_length = get_integer_input("How many days does the period last?: ")
  40. cycle_length = get_integer_input("What is the cycle length in days (from the start of one period to the start of the next)?: ")
  41.  
  42. # Calculate the next period and ovulation dates
  43. next_period, ovulation_start, ovulation_end = calculate_dates(last_period_date, period_length, cycle_length)
  44.  
  45. # Print the results
  46. print("\nNext Period Start Date:", next_period.strftime("%Y-%m-%d"))
  47. print("Estimated Ovulation Period: From", ovulation_start.strftime("%Y-%m-%d"), "to", ovulation_end.strftime("%Y-%m-%d"))
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement