compare items in list to x

asit lipun4u at gmail.com
Sun Nov 2 12:35:44 EST 2008


On Nov 2, 9:54 pm, "Pete Kirkham" <mach.... at gmail.com> wrote:
> 2 is not equal to '2'

As the error is correctly marked, u have to convert '2' to 2 by using
int() function

so the write code is

x = 2

def compare():
	for y in ['12', '33', '2']:
		y=int(y)  #string '2' is converted to int 2
		if x < y:
	            print x, "is less than", y
	        elif x > y:
	            print x, "is greater than", y
	        else:
	            print x, "and", y, "are equal"

compare()

Regards
Asit Dhal



More information about the Python-list mailing list