Coding Exercise for Beginners in Python| Exercise 5 | Python for Beginners #lec23
How to Calculate Remaining Days, Weeks, and Months Until 90?
Introduction to the Problem
- The video introduces a coding exercise focused on calculating how many days, weeks, and months are left for a person if they live until 90 years old.
- The program's output should be formatted as "You have A days, B weeks, and C months left," where A, B, and C are calculated values based on the user's current age.
Assumptions for Calculation
- Standard assumptions for calculations include:
- 1 year = 365 days
- 1 year = 52 weeks
- 1 year = 12 months
- Leap years are excluded from these calculations for simplicity.
Input Handling in Python
- The program will use the
input()function to take the user's current age.
- It is possible to directly typecast the input into an integer using
int(input()), which simplifies code by avoiding separate conversion steps.
Calculating Remaining Time
- To find out how many years are left until reaching age 90:
- Calculate:
years_left = 90 - current_age
- From this value:
- Days left:
days_left = years_left * 365
- Months left:
months_left = years_left * 12
- Weeks left:
weeks_left = years_left * 52
Output Formatting with F Strings
- The program prints a message displaying the calculated values. For example:
print(f"You have days_left days, weeks_left weeks, and months_left months left.")
- This formatting ensures that variables are correctly displayed within strings at runtime.
Example Execution of Program
- An example run of the program shows that entering an age of "85" results in:
- You have 1825 days, 260 weeks, and 60 months left.
- This demonstrates how users can easily calculate their remaining time based on their current age.