[Tutor] Stuck on some basics re floats

Ben Finney ben+python at benfinney.id.au
Wed Jul 18 04:07:51 EDT 2018

Matthew Polack <matthew.polack at htlc.vic.edu.au> writes:

> I'm a teacher trying to learn Python with my students.

Wonderful! Thank you for choosing Python for teaching your 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.

Yes. You are using the ‘input’ function, built in to Python 3

Read the documentation for that function; when successful, it returns
text (“a string”).

If you want an integer, you will need to explicitly tell Python to
convert the text to an integer value.

To create a new object, a general way is to use the type as a
constructor. The type you want is ‘int’, so use that as the constructor,
and you will (if it succeeds) get a new integer as the return value::

    response = input("Enter inches here: ")
    inches = int(response)

You will also need to learn about handling conversion errors: Try
entering something that is *not* a representation of an integer and see
what happens. How to handle the error is up to you.

 \     “Yesterday I parked my car in a tow-away zone. When I came back |
  `\                      the entire area was missing.” —Steven Wright |
_o__)                                                                  |
Ben Finney

More information about the Tutor mailing list