0

I will have a string and I want to check that it is in the format "float, float, float, float". There will be 3 commas and four floats, I don't know how many decimal places long the floats are gonna be. so I want to check it without using re class. I need to extract all four floats after checking the string. so 3 floats before a comma and 1 last after comma.

I can't find a string function to do that. I have checked my python reference book and still can't find a method. I usually code in C++ and recently begun Python. Thank you for your help.

2
  • 1
    And what is wrong with re? Homework assignment forbids it? Commented Feb 13, 2019 at 2:23
  • @Stephen Rauch kinda Commented Feb 13, 2019 at 2:28

1 Answer 1

1

Here's my attempt at your problem.

# Returns the list of four floating numbers if match and None otherwise
def four_float(str_input):
    # Split into individual floats
    lst = str_input.split(',')

    for flt in lst:
        # Check for the existence of a single dot
        d_parts = flt.strip().split('.')
        if len(d_parts) != 2:
            return None
        else:
            # Check that the chars on both sides of the dot are all digits
            for d in d_parts:
                if not d.isdigit():
                    return None

    return [float(n) for n in lst]
Sign up to request clarification or add additional context in comments.

Comments

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.