import sys
for line in sys.stdin.read().split("\n"):
text = line.strip()
try:
int(text)
kind = "integer"
except ValueError:
try:
float(text)
kind = "decimal number"
except ValueError:
kind = "text"
print(f"{text!r} is {kind}")
Output Verified by running it on Python 3
'42' is integer
'-7' is integer
'3.14' is decimal number
'1e3' is decimal number
'hello' is text
'12abc' is text
Try the strictest conversion first: int() fails on "3.14", float() accepts it (and scientific notation like 1e3), and anything that fails both is text.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!