[Tutor] Stuck on some basics re floats
alan.gauld at yahoo.co.uk
Wed Jul 18 03:28:12 EDT 2018
On 18/07/18 00:52, Matthew Polack wrote:
> 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")
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor