[Tutor] (no subject)

Alan Gauld alan.gauld at btinternet.com
Sat Jul 25 10:43:45 CEST 2015


On 25/07/15 02:49, Job Hernandez wrote:

> These lines of code don't work :
>
> a = raw_input('enter number: ')
> b = raw_input('enter number: ')
> c = raw_input('enter number: ')

raw_input() returns a string. So if you enter 6,
say, it is stored as the character '6' not the
number 6. You need to use the int() function to
convert it to an integer or float() to convert
it to a floating point number. eg.

c = int(raw_input('enter number: '))

> list = [ a, b, c]

A small point: its not a good idea to use the
type name as your variable because that then
hides the function used to convert things to lists.
ie you can no longer do

 >>> list('abc')
['a','b','c']

Its better to use names that describe the content
of the data, so in your case something like inputs
or numbers.

Just a small point which makes no difference in
this example but might trip you up in future.

> list2 =[ ]
>
> for x in list:
>    if x%2 == 1: # responsible for the type error: not all arguments

You get the error because you are trying to divide
a character by a number. If you convert to int()
up above then it will go away.

>        list2.append(x)
> print list2
>
> w = max(list2)
>

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