0

Please see the code below -

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

print "Let's do some math with just functions!"

age = add(float(raw_input("Add this:")), float(raw_input("To this:")))

Is there anyway, I can shorten the last line? Or, is there another way of getting user input?

Thanks

1
  • Rather than length of the line, you may want to worry about bad input from the user (like entering a string) Commented Nov 4, 2013 at 6:30

2 Answers 2

3

Applying "don't repeat yourself", we can take the repeated code and make a function out of it:

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

print "Let's do some math with just functions!"

def getf(prompt_mesg):
    s = raw_input(prompt_mesg)
    return float(s)

age = add(getf("Add this:"), getf("To this:"))

And then if you want you can make the input function handle errors better. Instead of raising an exception that takes down the whole program, you can handle errors gracefully:

def getf(prompt_mesg):
    while True:
        try:
            s = raw_input(prompt_mesg)
            return float(s)
        except ValueError:
            print("Could not convert that input.  Please enter a number.")

This will loop forever until the user enters a valid input (or terminates the program).

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

3 Comments

age = add(*(getf(s + " this:") for s in ("Add", "To"))). (SCNR)
Thanks for answering. Just one question. What is "floats" in return floats? Is that like a function?
float is the class in Python for floating point numbers. If you pass a string value to it, you are asking Python to make a float object, using the string... This is the way to convert a string to a float in Python. s is a variable name in my code example.
0

I see you're using p2.x. First thing - I'd recommend switching to p3.x (it has many enhancements, but in this case you'll be happy to see that raw_input() became input() and input () with evaluation is gone).

Other way to shorten this stuff is using input() instead of raw_input(). If user gives you anything that is not a number, you'll get some sort of exception at addition, if he gives you a number (float, int, whatever) - your program will work.

==EDIT==

As glglgl pointed out - second part is dangerous and warning here is appriopriate. Using input() is basically the same as eval(raw_input()). Unfortunately, I forgot about the fact, that it doesnt take locals and globals parameters like eval - if it would, you could make it safe. For real-life applications it shouldnt be used, because some rogue user could evaluate anything he wants, causing program (or even computer) to crash. For simple apps like that, I stand my ground, and keep saying that it is useful.

2 Comments

Part 2 is very dangerous if used without care. I am definitely missing a warning in here... And Part 1 is optional.
Yes, it is dangerous, if used mindlessly. And yes, you are missing a warning, my bad. Warning appended in edit ;)

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.