Python Forum

Full Version: While True is a syntax error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody,

I am working on a little NON AI-Chatbot and I wanted to make a "while True:" loop inside a "if name == "main"". But when I'm running the code, it says:

while True: 
^ SyntaxError: invalid syntax
I've tried making another script with a while True loop, and that worked, but in the other script it just doesn't work.

Here's the nessacery code:
if __name__ == "__main__"

    #forever loop

    while True:

    #here are some if input things


    #this is the last piece of code, in the if__name__ ...

    elif input() in query ["That's not funny at all", "That was not funny", "Not funny."]:
            jokesorry = ["I am sorry.", "Sorry! :-("]
            print(random.choice(jokesorry))
Yes, so when I run the code, I'm getting a syntax error cause of that while True: as I've menshiond above. But what am I doing wrong about the while True:. Why am I getting an error. Thanks for your help guys. I am using python 3.7.

Piethon
This is definitely syntax error and easy one - every if must end with :
A lot of times you have to look at the line before the traceback
(Jul-20-2019, 09:49 AM)perfringo Wrote: [ -> ]This is definitely syntax error and easy one - every if must end with :

I did that. Hm...I'm still getting that error.

(Jul-20-2019, 10:50 AM)metulburr Wrote: [ -> ]A lot of times you have to look at the line before the traceback

And where exactly? Do you mean this line: ?

if __name__ == "__main__"

Piethon
You posted if __name__ == "__main__". Did you added : to the end?
you posted this
if __name__ == "__main__"
It should be this
if __name__ == "__main__":
Quote:It should be this
if __name__ == "__main__":


I did it, and now, I'm getting a syntax error, cause of it.

if __name__ == "__main__":
^
SyntaxError: invalid syntax

Piethon
no, it's not because of it
sometimes the error is on the line before the one where the error is shown (note it points at the start of the line).
If you post your full code we may be able to tell you more. in any case - check the line before that one
It appears you have a similar issue above that line as well. You need to post the full code.