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

Python: conditional statement and function definition

I am using conditional statement in a function to ask weather an input is float/string/integer and display certain output if it determine each of the input but the input are all taken as string, how to tell the program to identify each input as string/float/integer?

this is the code below;

def strl(name):
    lname = len(name)
    return lname

name = input('please enter your name: ')
if type(name) == int:
    print("sorry, integars don't have a length")
elif type(name) == float:
    print('sorry, float do not have length ')
else:
    print(strl(name))
    print(type(name))

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

>Solution :

def strl(name):
    lname = len(name)
    return lname

name = input('please enter your name: ')

try:
    int(name)
    print("sorry, integars don't have a length")
except:
    try:
        float(name)
        print('sorry, float do not have length ')
    except:
        print(strl(name))
        print(type(name))
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