[Tutor] Stuck on some basics re floats

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jul 18 03:28:12 EDT 2018


On 18/07/18 00:52, Matthew Polack wrote:
> Hi,
> 
> I'm a teacher trying to learn Python with my students.
> 
> I am trying to make a very simple 'unit calculator' program...but I get an
> error ..I think python is treating my num1 variable as a text string...not
> an integer.

You are correct.

> How do I fix this?

Convert the string to a float:

f = float(s)

> print ("How many inches would you like to convert? ")
> num1 = input('Enter inches here')

In Python 3 input() always returns a string.
In python 2 input() returns an evaluated version of the
string, so if a number was entered Python returned a number.
This was a serious security risk however, so input() was
removed in Python 3 and the old raw_input() was
renamed as input().


> print ("You have entered",num1, "inches")
> convert = num1 * 2.54

convert = float(num1) * 2.54

> print ("This is", convert, "centimetres")
HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list