April 11th, 2013, 03:36 PM

Syntax error ()
hy everyone. i'm a beginner in python, also in any programing, so i request your patience
here is my syntax error(got the blank space after code in red):
Code:
>>> for i in range (1,999):
print(i) if (i/3==int)
SyntaxError: invalid syntax
what i want to do is to check if the number can divide by 3.
it would be handy if you guys tell me how to add these to a sum
April 11th, 2013, 04:17 PM

First you define the requirement, then you define what's gonna happen if the requirement is fulfilled,
So
Code:
if i/3 == int:
print i
Tho, that won't work cuz you can't check types like that, I'd use the builtin function is_integer() for this:
Code:
for i in range(1, 99):
if (i/3.0).is_integer():
print i
my terminal window doesn't hold all 999 solutions, so i scaled it down to 99.
Good luck
Comments on this post
April 11th, 2013, 04:25 PM

Code:
# looks like you're using python3.
# this will fix your issue (except that I'll bet you think 999 should be in the output)
for i in range (1,999):
print(i) if i/3 == int(i/3) else print(end='')
# an alternative, see the modulus operator
# and also that "not 0" is true while "not any_other_number" is False
# This way uses only one division
for i in range (1,999):
print(i) if not i%3 else print(end='')
# but to sum them you can use an implicit iterator
print(sum(i for i in range (1,999) if not i%3))
# now recognizing that False and True can be used as numbers 0 and 1
# we get to my favored solution
print(sum(i*(not i%3) for i in range (1,999)))
# times being the way they are,
# someone smart will know to change the stride in range
# In this case I had to change the start value
print(sum(i for i in range (3,999,3)))
#################
#You may have originally intended
for i in range (1,999):
print(i) if type(i/3) is int else print(end='')
#Which fails because the type of the division quotient is float
[code]
Code tags[/code] are essential for python code and Makefiles!
April 11th, 2013, 04:35 PM

Oh yeah, don't mind my comment then, i completely overlooked the parenthesis stuff and didn't realize it was python 3.
April 12th, 2013, 09:02 AM

In my opinion, your post, Lucantrop, was brilliant. You showed me something new. I had to doubletake before I understood.
[code]
Code tags[/code] are essential for python code and Makefiles!
April 12th, 2013, 09:20 AM

Yeah, that's a nice way to get float numbers out of a division.
Anyhow, thanks, glad I could help.
April 14th, 2013, 02:03 AM

thank you guys. that was awesome
anyway, my little project just got more complicated, si i need to store the values of i in a list, and then to make the sum of numbers in the list.
2nd question: how can i compare the numbers in 2 lists, and eliminate the duplicates.
i guess anyone knows now that i want to make the sum of the numbers divisible by x and y from the interval i, but i just don't want the code. i want to figure it out myself
April 14th, 2013, 04:01 AM

You can basically just use a builtin function sum() on your list.
As for your second question, using nested for loops is your best bet. Look 'em up.
Hope i didn't spoil too much or too little.
April 14th, 2013, 01:47 PM

where is the "thanks" button when you need it?
edit:
here is my code so far, but i cant see how to use nesting in these 2 list of my code, so i figured it out in another way
Code:
>>> ================================ RESTART ================================
>>> x = [i for i in range(3,1001,3)]
>>> y = [i for i in range(5,1001,5)]
>>> xy = sum(x) + sum(y)
>>> xydoubles = [i for i in range(15,1001,15)]
>>> finalsum = xy  sum(xydoubles)
>>> finalsum
234168
>>>
i would highly apreciate if some of you guys can tell me how to use nesting in list, because in my case, the code is limited to this situation, and deducing the double numbers in another list will not be that easy
2nd edit:
yaaay!!! i just solved the first problem from project euler.
big thanks for everyone who helped me understanding py syntax
Comments on this post
April 15th, 2013, 09:41 AM

bump check my edit, sorry for doublepost
April 15th, 2013, 10:42 AM

Code:
list1 = [i for i in range(3, 1001, 3)]
list2 = [j for j in range(5, 1001, 5)]
list3 = []
for i in list1:
for j in list2:
if i == j:
list3.append(j) # add duplicates to list3
print sum(list1 + list2)  sum(list3)
That's nested loops, but a much shorter solution would be:
Code:
list1 = [i for i in range(3, 1001, 3)]
list2 = [j for j in range(5, 1001, 5) if j not in list1]
print sum(list1 + list2)
Also:
Code:
print sum([i for i in range(3, 1001, 3)]+[j for j in range(5, 1001, 5) if j not in range(3, 1001, 3)])
Same as 2nd, but in one line.
The second one is my favorite tho.
April 16th, 2013, 12:20 PM

tnx.
i love python for being that flexible it just put your brain at work
Comments on this post