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

How can I check is there any number in series and count any number(float,integer etc)?

Here is my code:
I check number with isnumeric() function but it only return integer number is there any other way to count any number(it doesn’t matter if it is integer or float) in series?

def check_number(item):
    try:
        num1=float(item)
    except:
        return 'Not number'
       

series=input('Enter a series of numbers separated by a space:').split()
count_numbers=0
count_non_numeric=0
for item in series:
    print(check_number(item))
    if item.isnumeric():
        count_numbers+=1
    else:
        count_non_numeric+=1
print('Amount of numbers: ',count_numbers)
print('Amount of non-numeric values: ',count_non_numeric)

I try to write it with the try and except and create check_number() function but I couldn’t get the result that I want. I want my program to check the series if there isn’t any number then my program return "no number" else it returns nothing.

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 :

It looks like you haven’t returned anything in your function. This change should make your program work:

def check_number(item):
    try:
        return float(item)
    except:
        return 'Not number'

I have just replaced your variable assignment with a return statement.

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