Re: [Tutor] Stuck on some basics re floats

2018-07-18 Thread Alan Gauld via Tutor
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...


-- 
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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stuck on some basics re floats

2018-07-18 Thread Ben Finney
Matthew Polack  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
https://docs.python.org/3/library/functions.html#input>.

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stuck on some basics re floats

2018-07-18 Thread Peter Otten
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.
> 
> How do I fix this?
> 
> Thanks!
> 
> - Matt
> 
> print ("How many inches would you like to convert? ")
> num1 = input('Enter inches here')

As you have guessed, at this point num1 is a string. You can convert it to a 
float with

num1 = float(num1)

> print ("You have entered",num1, "inches")
> convert = num1 * 2.54
> print ("This is", convert, "centimetres")
> 
> 
> Traceback (most recent call last):
>   File "convert.py", line 10, in 
> convert = num1 * 2.54
> TypeError: can't multiply sequence by non-int of type 'float'

Note that int is mentioned here because Python can multiply int with str, 
though the result might surprise you:

>>> "spam" * 3
'spamspamspam'

So * also works as the repetition-operator just like + is also used to 
concat strings.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stuck on some basics re floats

2018-07-18 Thread Alan Gauld via Tutor
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Stuck on some basics re floats

2018-07-18 Thread Matthew Polack
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.

How do I fix this?

Thanks!

- Matt

print ("How many inches would you like to convert? ")
num1 = input('Enter inches here')
print ("You have entered",num1, "inches")
convert = num1 * 2.54
print ("This is", convert, "centimetres")


Traceback (most recent call last):
  File "convert.py", line 10, in 
convert = num1 * 2.54
TypeError: can't multiply sequence by non-int of type 'float'


Matthew Polack | Teacher


[image: Emailbanner3.png]

Trinity Drive  |  PO Box 822

Horsham Victoria 3402

p. 03 5382 2529   m. 0402456854

e. matthew.pol...@htlc.vic.edu.au

w. www.htlc.vic.edu.au

-- 
**Disclaimer: *Whilst every attempt has been made to ensure that material 
contained in this email is free from computer viruses or other defects, the 
attached files are provided, and may only be used, on the basis that the 
user assumes all responsibility for use of the material transmitted. This 
email is intended only for the use of the individual or entity named above 
and may contain information that is confidential and privileged. If you are 
not the intended recipient, please note that any dissemination, 
distribution or copying of this email is strictly prohibited. If you have 
received this email in error, please notify us immediately by return email 
or telephone +61 3 5382 2529** and destroy the original message.*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor