### Thread: Syntax error ()

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0

#### 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
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
6
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 built-in 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

• b49P23TIvg agrees : Interesting. I was unaware of this method of float.
3. 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```
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
6
Oh yeah, don't mind my comment then, i completely overlooked the parenthesis stuff and didn't realize it was python 3.
5. In my opinion, your post, Lucantrop, was brilliant. You showed me something new. I had to double-take before I understood.
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
6
Yeah, that's a nice way to get float numbers out of a division.
Anyhow, thanks, glad I could help.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
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
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
6
You can basically just use a built-in 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.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
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

• b49P23TIvg agrees : PE#1. Nice work!
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
bump check my edit, sorry for double-post
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
6
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.
12. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
5
Rep Power
0
tnx.
i love python for being that flexible it just put your brain at work

#### Comments on this post

• Lucantrop agrees : Yep, it's awesome like that