[Tutor] Stuck on some basics re floats
alan.gauld at yahoo.co.uk
Wed Jul 18 12:05:31 EDT 2018
On 18/07/18 14:10, Matthew Polack wrote:
> Thanks for the reply Alan.
> I found another fix where I could just use this:
> num1 =int(input('Ener inches here?: '))
> but a lot of people like yourself.... seem to be recommending this
> 'float' method.
It all depends whether your input is a float or an integer.
If it will always be a whole number of inches then use int().
If it will be a floating point value then you need to use float().
In your example I'd have thought it reasonable to allow
conversions of, say, 11.5 inches...
I meant to add that its generally considered good practice
to do the conversion as early as possible unless you plan
on using the string version later. So wrapping the input()
call inside the int or float call is good.
You can then wrap that inside a try/except and get the user
to re-try if they type invalid data - if you so wish...
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor