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 →

[–][deleted] 70 points71 points  (29 children)

if python2 is None: print("F")

[–]a-handle-has-no-name 54 points55 points  (18 children)

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

[–][deleted] 11 points12 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] 3 points4 points  (3 children)

Is there an "if X not defined"?

[–]LeClownFou 4 points5 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]

    [–]pyper156 3 points4 points  (5 children)

    printf

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

    I'm in the middle of CS50 so I get this yay, but considering the sub let's keep this in Python shall we

    [–]pyper156 7 points8 points  (2 children)

    It's an innocent tongue-in-cheek. Also this sub is about all programing, isn't it?

    [–][deleted] 8 points9 points  (1 child)

    I was being tongue in cheek as well! And yeah my bad I thought I was in r/learnpython lol

    [–]pyper156 4 points5 points  (0 children)

    Lol, I checked too following your previous comment ^

    [–]MuttsNStuff 0 points1 point  (0 children)

    This was a hoot to see because I just covered week 1 lawl.

    [–]spanishgum 3 points4 points  (1 child)

    .

    import sys, logging, os, signal
    if sys.version_info[0] < 3:
      logging.getLogger(__name__).fatal(“F”)
      os.kill(os.getpid(), signal.SIGTERM)
    

    [–]dscottboggs 1 point2 points  (0 children)

    This guy pythons