1

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)
1
  • edit to have human_age as one line: human_age = int(input(....), why 0-2 is there a better variable choice? what's the issue with first choice, both cases do make use of boolean anyways with the help of if statement. Commented Apr 30, 2020 at 16:27

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Im sorry for asking such a dumb question, but why are you using lambda? Thanks in advance
@dev, it's not essential, just helps to shorten the code in this case. I'll update the code to work without lambda as well.
Really appreciate your comments on style. Would you mind telling me if for this particular mini script am I better off using the if else with booleans as per my original program at the very top. When would it be good practise to use your lambda version?
Almost always better of not using lambdas. I rarely use those in production code myself. Remember - Simple is better than complex. Complex is better than complicated.
-1

I think it’s enough to use

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

2 Comments

Might be a different language though
That doesn't even compile. And misses half the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.