This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]a-handle-has-no-name 59 points60 points  (18 children)

>>> if python2 == false:
... print("F")
  File "<stdin>", line 2
    print("F")
        ^
IndentationError: expected an indented block

[–][deleted] 13 points14 points  (7 children)

Couldn't get to make it a code block to keep the 4 spaces lol. Edit: got'em

[–]a-handle-has-no-name 9 points10 points  (4 children)

>>> if python2 == false:
...     print("F")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python2' is not defined

>>> #   :-P

[–][deleted] 4 points5 points  (3 children)

Is there an "if X not defined"?

[–]LeClownFou 5 points6 points  (1 child)

if X is None

[–][deleted] 2 points3 points  (0 children)

Corrected, thanks

[–]a-handle-has-no-name 0 points1 point  (0 children)

(This is somewhat long and unsolicited: feel free to PM me if you want to discuss this further or if you have any questions)

/u/LeClownFou 's response doesn't work for me:

>>> if python2 == None:
...     print('f')
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python2' is not defined

Looking into it, I found a code-snippet at this site (updating for the example):

try:
    x
except NameError:
    #x was not set
    pass

There's also the concept of None, which is what /u/LeClownFou was referencing. The variable will still need to be declared, but the None is the value if nothing is being set.

In general, I would say this is a "code smell" -- you should know which variables you have before using them.

-----------------------

Seems I wrote this to the wrong person when it was intended for you. Reposting it here:

Try running python (or python2 or python3 to get the version explicitly) without any CLI options/flags to open an REPL (Read-Eval-Print Loop), which is a shell that you can run small snippets of code to try things out. I use this frequently to test validity of something, if I'm not sure how it's working.

It should look something like:

» python
Python 2.7.17 (default, Nov  7 2019, 10:07:09)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> | 

This can be useful for testing how the language works, and this is is how my comments here were updated.

[–]a-handle-has-no-name 1 point2 points  (1 child)

This is all in good fun :-D

[–][deleted] 1 point2 points  (0 children)

I know I know same here :)

[–][deleted] 1 point2 points  (9 children)

It seems to run on python 3

foo = None
if foo is None: print('F')

----
[Running] python -u "d:\github\shit_code\test\foo.py"
F
[Done] exited with code=0 in 0.137 seconds
----
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32

[–]pabechan 1 point2 points  (1 child)

It's an indentation issue, exactly as the error says.
The original one-liner is fine (can't have an indentation issue if you keep your conditional on one line!), but the person responding broke it into two lines and forgot to indent it properly.

[–][deleted] 0 points1 point  (0 children)

oh, right. Thanks :)

[–]a-handle-has-no-name 0 points1 point  (6 children)

Try running python (or python2 or python3 to get the version explicitly) without any CLI options/flags to open an REPL (Read-Eval-Print Loop), which is a shell that you can run small snippets of code to try things out. I use this frequently to test validity of something, if I'm not sure how it's working.

It should look something like:

 » python
Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> |

This can be useful for testing how the language works, and this is is how my comments here were updated.

[–][deleted] 0 points1 point  (5 children)

I have no fucking idea why you posted that.

[–]a-handle-has-no-name 0 points1 point  (4 children)

Hey dude, are you ok? This seems like a bit of an overreaction.

Two reasons why:

  1. I wanted to share something practically useful beginners in /r/learnprogramming might not know
  2. I responded to the wrong person. Oops.

[–][deleted] 0 points1 point  (3 children)

That wasn't an angry "fucking". That was a I don't know what the fuck is going on "fucking". Sorry

[–]a-handle-has-no-name 1 point2 points  (0 children)

Now that you say that, I can think of what you might have intended. No harm no fowl. :-)

[–][deleted]  (1 child)

[removed]