Python Forum
While True is a syntax error? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: While True is a syntax error? (/thread-19924.html)



While True is a syntax error? - Piethon - Jul-20-2019

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


RE: While True is a syntax error? - perfringo - Jul-20-2019

This is definitely syntax error and easy one - every if must end with :


RE: While True is a syntax error? - metulburr - Jul-20-2019

A lot of times you have to look at the line before the traceback


RE: While True is a syntax error? - Piethon - Jul-20-2019

(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


RE: While True is a syntax error? - perfringo - Jul-20-2019

You posted if __name__ == "__main__". Did you added : to the end?


RE: While True is a syntax error? - metulburr - Jul-20-2019

you posted this
if __name__ == "__main__"
It should be this
if __name__ == "__main__":



RE: While True is a syntax error? - Piethon - Jul-21-2019

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


RE: While True is a syntax error? - buran - Jul-21-2019

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


RE: While True is a syntax error? - metulburr - Jul-21-2019

It appears you have a similar issue above that line as well. You need to post the full code.