[Tutor] repply

Ole Henning Jensen tjampman at gmail.com
Sun Jan 4 17:06:12 CET 2009


prasad rao wrote:
> hi 
>   I got it right.
> 
>  >>> z=[]
>  >>> for x in range(1000):
>     if divmod(x,3)[1]==0:z.append(x)
>     if divmod(x,5)[1]==0:z.append(x)
> 
>  >>> sum(set(z))
> 233168
> 

Instead of using the set function you could just use an elif in your for 
loop.

 >>> z=[]
 >>> for x in range(1000):
	if divmod(x,3)[1]==0:z.append(x)
	elif divmod(x,5)[1]==0:z.append(x)

	
 >>> sum(z)
233168

or as somebody else suggested use an OR operator

 >>> z=[]
 >>> for x in range(1000):
	if (divmod(x,3)[1]==0) or (divmod(x,5)[1]==0):
		z.append(x)

	
 >>> sum(z)
233168

just some variations... On an other wise correct anwser.


More information about the Tutor mailing list