on-internet
← Community

How can I validate if a string is a real number in Python using string methods? [duplicate]

Stack Overflow ·

The assignment: Write a Python program that reads text from the keyboard and uses string manipulation methods to validate whether the input is a valid real number. Valid examples: -4.313, 0.341, 13, -3, 1.5, 2., .6 Invalid examples: --123, 1-21, 1..3, 12..., .-21 My latest attempt: number = eval(input("Enter a number:")) while type(number) == float: if number < 0 or number > 0: print(f"Numarul {number} este real") break Previously, I also tried using isdecimal(), but that didn't recognize negative numbers or decimal numbers containing a point. I understand that my current approach doesn't corr

Comments

Sign in or sign up to comment and upvote.

No comments yet.