Maths with Ternary Boolean in Python

Question:

I wrote this little script to calculate dog years. The first two dog years are 10.5 human years and all other years following are worth 4.

human_age = int(input("Enter the human age to convert it to doggy years: "))
    valid = (human_age <=2)
    def convert_to_dog_years(human_age):
        if valid:
            dog_years = human_age * 10.5
            print('Dog age is:', int(dog_years))
        elif not valid:
            dog_years = ((((human_age - 2) * 4) + 21))
            print('Dog age is:', int(dog_years))
    convert_to_dog_years(human_age)

I was thinking something more along those lines:
I would like to specify the mathematical operations giving them two names one for numbers from 0-2 and the other from 2 and up.
Then I would like to use a boolean to decide which math process I want to apply.

Is this possible in python?

0-2    = dog_years = human_age * 10.5
>=2    = dog_years = ((((human_age - 2) * 4) + 21))

human_age = int(input("Enter the human age to convert it to doggy years: "))
valid = (human_age <=2)
def convert_to_dog_years(human_age):
    if valid 0-2 else 2&up
        print('Dog age is:', int(dog_years))
convert_to_dog_years(human_age)
Asked By: dev

||

Answers:

While, the question is not clearly articulated, It seems you are looking at ways to store functions and call these conditionally.

Good news is that, in python functions are first class object.

So you could do something like this –

>>> handlers={
...  'valid':lambda human_age:human_age * 10.5,
...  'invalid': lambda human_age:((((human_age - 2) * 4) + 21))}
>>> handler_key = 'valid' if human_age <=2 else 'invalid'
>>> human_age=3 #In your case, take input here
>>> print(handlers[handler_key](human_age)) #call handler
25

To further respond to OPs comment, lambda’s are not essential here by any means. Following is the same code with simple functions –

>>> def invalid_handler(human_age): return ((((human_age - 2) * 4) + 21))
... 
>>> def valid_handler(human_age): return human_age * 10.5
... 
>>> handlers = {
...  'valid': valid_handler,
...  'invalid': invalid_handler}
>>> 
>>> print(handlers[handler_key](human_age))
25
>>> human_age=1
>>> print(handlers[handler_key](human_age))
17

I would also take this opportunity for a short rant against python and almost all modern programming languages in general –

In modern programming languages, Why are there so many ways to do the same thing?

Python Zen, no. 13, states –

There should be one– and preferably only one –obvious way to do it.

Yet, there are multiple ways to achieve the same results. I really wish modern programming languages kill the temptation to add as many features as possible and instead focus on doing the most important job better – Speed, tooling, better versioning, frameworks.

I come from ‘C’ background and I believe it is the best programming language created till date.

Answered By: mahoriR

I think it’s enough to use

age = input("How old are you? ").val
print "That's " + age*7 + " in dog years."
Answered By: AcidGamerX