Editor

Tidy a Number

#574 · Python Basics · Beginner · 35 XP · about 5 min

A web form sends every answer as text, so an age arrives as '14' and a price as '12.50'. Write tidy_number(text) that turns the text into a number:

  • If the number is whole, return it as an int: '14' gives 14, and '4.0' gives 4 too.
  • Otherwise return it as a float: '12.50' gives 12.5.

The text may have spaces around it. The tests check the type of the answer as well as its value.

Examples

tidy_number('14')     # returns 14
tidy_number('12.50')  # returns 12.5
tidy_number('4.0')    # returns 4
tidy_number('-3')     # returns -3

Note

text always holds an ordinary number, such as '7', '-2.5' or ' 0.25 '.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.