0

so far I have this:

import datetime
f = open("log.txt", "a", encoding='UTF-8')
print ("Log file created")
print ("Enter /close to leave.")
spc = " "
while 1:
    msg = input(">>:")
    now = datetime.datetime.now()
    now1 = str(now)
    if msg == None:
        pass
    if msg == " ":
        pass
    else:
        msg2 = now1+spc+msg+"\n"
        if msg == "/close":
            exit()
        f.write(msg2)
        f.flush()

However, this line is not functioning as I want it, it still returns a blank line on the log file:

if msg == None:
    pass

I want it to not return anything and simply continue the while loop, How would I fix this?

cjmaria
  • 330
  • 4
  • 16

4 Answers4

1

You should be using

if msg is None:
    pass

Edit

You're missing what the pass function is all about. I would re-write your look like so. This way we're only processing this if the msg is not one of the bad input. Once we're done we break out of the loop.

...
while 1:
    msg = input(">>:")
    now = datetime.datetime.now()
    now1 = str(now)
    if not msg in [None, " "]
        msg2 = now1+spc+msg+"\n"
        if msg == "/close":
            exit()
            f.write(msg2)
            f.flush()
            break
Slick
  • 359
  • 1
  • 8
  • 1
    `None == None`, so this won't make a difference. – jwodder Jun 28 '14 at 04:01
  • perfect, thanks. I guess it was just a mistake on my part (its my 3rd day with python) – cjmaria Jun 28 '14 at 04:03
  • @ChrisM. You'll find more flow control example at this link. https://docs.python.org/3/tutorial/controlflow.html – Slick Jun 28 '14 at 04:29
  • @jwodder - My thinking, as well. In fact, using 'is' instead of '==' could have unintended consequences : http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python – arvindch Jun 28 '14 at 04:33
  • I believe you mean "`not in`", not "`is in`" (which isn't even valid Python). – jwodder Jun 28 '14 at 04:38
  • @shadow the "is None" comparison is called out in PEP8 "Comparisons to singletons like None should always be done with is or is not, never the equality operators." – Slick Jun 28 '14 at 04:42
  • 1
    @Slick - I'd forgotten that, thanks! Regardless, looking at the source code, how exactly would msg be None, anyway? raw_input() will return a string, no matter what, right? Seems like checking for None is unnecessary. – arvindch Jun 28 '14 at 04:51
0

Evaluation of the rest of the loop will still continue after pass, and as None does not equal " ", this means the block beginning with msg2 = now1+spc+msg+"\n" will be executed. You need to either unite the if ... if ... else into a single if block by changing if msg == " ": to elif msg == " ": or else change if msg == None: pass to if msg == None: continue.

jwodder
  • 54,758
  • 12
  • 108
  • 124
0
try:
    msg2 = now1+spc+msg+"\n"
    if msg == "/close":
        exit()
    f.write(msg2)
    f.flush()

except:
    pass #example for a function: return None or raise
wazo
  • 311
  • 2
  • 16
-1

Your condition doesn't make any sense. The input function will never return None, only strings.

If you want to skip empty strings, a better test would be if not msg (empty strings are "falsy"). Or, if you want to reject any all-whitespace strings, try if not msg.strip() (which removes leading and trailing whitespace before checking if the rest of the string is empty or not).

Further, it's rarely a good idea to write an if statement that just contains pass. Instead, invert the test so that the condition is true for cases where you want to run some code (in this case, when msg is not empty or all whitespace) and simply omit the cases where you'd do nothing:

while 1:
    msg = input(">>:")
    now = datetime.datetime.now()
    now1 = str(now)
    if msg.strip():               # msg is not empty or all whitespace
        msg2 = now1+spc+msg+"\n"
        if msg == "/close":
            exit()
        f.write(msg2)
        f.flush()

One final issue (unrelated to the main question). Python's exit function is primarily intended for use in the interactive interpreter. It is added to the builtins by the site module, and so it won't exist if Python was run with the -S flag. If you want to close the interpreter, you should instead call sys.exit, raise a SystemExit exception, or just run off the end of the main module (a break statement would probably do that for the loop you've shown here, or perhaps a return if you're in a function somewhere).

Blckknght
  • 100,903
  • 11
  • 120
  • 169