Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue detecting if an input contains both letters and numbers

When running this code below with input that includes both letters and numbers it prints not…

# test for both numbers and letters
def multi_test(analysis_input):
    alpha_test = False
    number_test = False
    if analysis_input.isalpha():
     alpha_test = True
     if analysis_input.isnumeric():
        number_test = True
        if alpha_test and number_test:
         print(analysis_input,'is multiple charcters')
        else:
         print('not')



analysis_input = input('enter your string arguement')
multi_test(analysis_input)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to check for each character:

def multi_test(analysis_input):
    alpha_test = False
    number_test = False
    for char in analysis_input:
        if char.isalpha():
            alpha_test = True
        if char.isnumeric():
            number_test = True
    if alpha_test and number_test:
        print(analysis_input,'is multiple charcters')
    else:
        print('not')



analysis_input = input('enter your string arguement')
multi_test(analysis_input)

EDIT: a perhaps faster and nicer method is using regex

^(?=.*[a-zA-Z]+)(?=.*\d+).+
# pseudo code, i'm not sure if this code works but its something like this
return bool(re.match(analysis_input, ^(?=.*[a-zA-Z]+)(?=.*\d+).+))

Idea from https://stackoverflow.com/a/24656216/10875953

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading