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

Question:

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.

Asked By: Luna X

||

Answers:

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.

Answered By: EthanFrancis
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.