Search code examples
pythonstringuser-inputbounding

Using multiple inputs for floats?


Writing an application that needs to build a bounding box. The box is built by obtaining the South West corner and the North East corner of the desired box. The application is expecting this code in the format of xx.xxx,xx.xxx.

Started with this for example:

southwest_corner = float(raw_input("Enter the SW corner values: "))

But it does not accept the fact that it has the comma and second value. (remember xx.xxx,xx.xxx) I also tried it as a string instead of a float, but still no-go.

How do I allow the user to input the format I want and it properly take it?


Solution

  • I fixed this issue by calling map and adding a .split to the end of my input lines:

    southwest_corner = map(float, raw_input("Define the SW corner of your box: ").split(','))
    

    This allowed me to properly input my expected format of xx.xxxx,xx.xxxx.

    map applied the function (in this case float) across a collection of items. I made it a string by separating the input at the comma by calling .split(','). So the input was split into a string, and then map mapped that input into float.

    This thread was helpful: Python 2.7 - Invalid Literal Errors