mercredi 28 mars 2018

Working with blank user input in if statement

Im working on a program which takes three inputs from the user, "Street number: ", "2nd Street number (If applicable): " and "Street name: " and return a "True" or "False" result.

Not all the addresses in the work I'm doing have a second street number value so I wanted to have the option of leaving that input blank. However i've run into the issue where if do I leave the second input blank I get the valueError result "False" instead of the "True" result.

I've tried using isspace() but that hasn't worked. Are there any ideas on how I can fix this?

def valid_address(int_address, int_address2, str_address):

while True:
    try:

        val1 = int_address
        val2 = int(val1)
        val3 = int(int_address2)
        val4 = int_address2
        val5 = val4 + ""
        str_address

        if( val2 > 0  ):
            if( val3 > 0 ) or ( val5.isspace() ):
                if not( str_address.isdigit() ):
                    return("True")

        return("false") 

    except ValueError:
        return("False")



print(valid_address(int_address = input("Street number: "), 
                    int_address2 = input("2nd Street number (If applicable): "), 
                    str_address = input("Street name: ")))

Aucun commentaire:

Enregistrer un commentaire