Posts: 404
Threads: 94
Joined: Dec 2017
May-10-2018, 11:19 PM
(This post was last modified: May-10-2018, 11:21 PM by Truman.)
x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if (((i+j) !=n))) The goal is to print all possible coordinates.
I receive a generator object message. Don't understand why.
and when I change print line to
print[[x,y,z] [b]for [/b]i in range(x+1) for j in range(y+1) for z in range(z+1) if ((x+y+z) !=n )] it gives "SyntaxError: invalid syntax" message pointing to bolded for.
Posts: 2,342
Threads: 62
Joined: Sep 2016
Are you using Python 3? If so, print needs parenthesis.
Posts: 536
Threads: 0
Joined: Feb 2018
May-11-2018, 05:14 PM
(This post was last modified: May-11-2018, 05:15 PM by woooee.)
Did you declare z as as an int earlier in the program. If so, please post that code as well.
Posts: 566
Threads: 10
Joined: Apr 2017
(May-10-2018, 11:19 PM)Truman Wrote: x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if (((i+j) !=n))) The goal is to print all possible coordinates.
I receive a generator object message. Don't understand why.
Add another set of angular brackets in a print statement - within and next to outermost brackets.
And (((i+j) !=n)) is extremely ugly and un-Pythonic.
i + j != n is the proper Pythonic form. I sentence you to read PEP-8
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 404
Threads: 94
Joined: Dec 2017
@ micseydel
yes, I use P3
@ woooee
In pasting code I mixed 2D with 3D program, sorry for that. I was referring to x,y program that doesn't work.
@ volcano63
It may sound stupid but I've never heard for term "angular brackets". The closest that google gives are angle brackets <>. Do you mean square brackets?
And thank you for PEP-8 link, will start reading it immediately.
Posts: 566
Threads: 10
Joined: Apr 2017
(May-11-2018, 09:30 PM)Truman Wrote: Do you mean square brackets?
My bad, exactly. Brackets is not an issue I often discuss in English
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 404
Threads: 94
Joined: Dec 2017
May-12-2018, 11:13 PM
(This post was last modified: May-12-2018, 11:13 PM by Truman.)
x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if i+j != n) I just don't understand what's wrong with this code. :(
Finally got it
x = int(input())
y = int(input())
n = int(input())
print([[i,j] for i in range(x+1) for j in range(y+1) if i+j != n]) Do we always use [] for list comprehensions?
Now, doing a 3D version:
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n ]) I receive a ValueError: invalid literal for int() with base 10: ''
Posts: 2,342
Threads: 62
Joined: Sep 2016
That will happen if you enter an empty line instead of an int.
Also when you get an error message please make sure to include your full stacktrace, not just one line from it.
Posts: 404
Threads: 94
Joined: Dec 2017
May-13-2018, 10:26 PM
(This post was last modified: May-14-2018, 06:10 AM by buran.)
Error: Traceback (most recent call last):
File "C:\Python36\kodovi\koordinate.py", line 10, in <module>
print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n])
File "C:\Python36\kodovi\koordinate.py", line 10, in <listcomp>
print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n])
UnboundLocalError: local variable 'z' referenced before assignment
This is what I receive for code:
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n])
Posts: 2,342
Threads: 62
Joined: Sep 2016
That's a weird error to be getting, but you can solve it by not re-using variable names (you reuse z here). Take your list comprehension and write a nested loop instead. Only after you've gotten that working should you try putting it back into the more advanced form.
|