I was wondering if how can python solve this mathematical equation for the variable "yearsUntilAgeIsDoubled". It is a variable whose value will depend on the input of the user (age of father and age of user).
userAge = int(input("How old are you?"))
fatherAge = int(input("How old is your father"))
yearsUntilAgeIsDoubled = " "
fatherAge yearsUntilAgeIsDoubled = 2 * (userAge yearsUntilAgeIsDoubled)
userAgeNew = userAge yearsUntilAgeIsDoubled
fatherAgeNew = fatherAge yearsUntilAgeIsDoubled
if yearsUntilAgeIsDoubled.isnumeric:
print(f'Your fathers age will be double of yours in {yearsUntilAgeIsDoubled} years. You will be {userAgeNew} years old.')
else:
print("Your fathers age will never be the double of yours.")
CodePudding user response:
Do you mean
yearsUntilAgeIsDoubled = fatherAge - 2 * userAge
CodePudding user response:
If you're just interested in the year for when the father will be twice the age of the user, then you need to define a timeline. For example, this graph's timeline starts at the year the user was born.
You can find where the lines intersect by setting the green line's equation equal to the orange line's equation. Let d = father's age - user's age:
- Blue Line: y = x
- Orange Line: y = x d
- Green Line: y = 2x
2x = x d ---> x = d
So, the father's age is double the user's age at "d" years from the birth of the user. Again, this all depends on how you want to interpret the time at which the age is doubling the user. If you wanted to find the time relative to the current age of the user, then you would say, "The father was (or will be) twice the age of the user at {current age - d} years ago (or from now)." If current age - d is positive, then it would have occurred current age - d years ago; if current age - d is negative, then it will occur |current age - d| years from now.
Example Code
user_age = int(input('How old are you? '))
father_age = int(input('How old is your father? '))
d = father_age - user_age
assert d > 0, 'Your father cannot be younger than you or the same age.'
if user_age - d > 0:
print(f'Your father was doubled your age {user_age - d} years ago.')
elif user_age - d < 0:
print(f'Your father will be doubled your age {d - user_age} years from now.')
else:
print('Your father will be doubled your age this year.')
Example Output
How old are you? 12
How old is your father? 40
Your father will be doubled your age 16 years from now.
CodePudding user response:
Not sure why you'd want the yearsUntilAgeIsDoubled to be a string. This can be done easier if it's an integer.
userAge = int(input("How old are you?\n"))
fatherAge = int(input("How old is your father?\n"))
if fatherAge - (2 * userAge) == 0:
print("Your fathers age is the double of yours now.")
elif fatherAge - (2 * userAge) > 0:
yearsUntilAgeIsDoubled = fatherAge - (2 * userAge)
userAge = yearsUntilAgeIsDoubled
print(f"Your fathers age will be double of yours in {yearsUntilAgeIsDoubled} years. You will be {userAge} years old.")
else:
print("Your fathers age will never be the double of yours.")
Also, this code doesn't check if any of the ages are negative, but you could make an if statement if you want.

