Python Forum
While True is a syntax error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While True is a syntax error?
#1
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
Reply
#2
This is definitely syntax error and easy one - every if must end with :
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
A lot of times you have to look at the line before the traceback
Reply
#4
(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
Reply
#5
You posted if __name__ == "__main__". Did you added : to the end?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
you posted this
if __name__ == "__main__"
It should be this
if __name__ == "__main__":
Reply
#7
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
Reply
#8
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function syntax error Changosoyyo 2 238 Mar-07-2021, 10:42 PM
Last Post: Changosoyyo
  Syntax error on line 29 manwithjeans 3 258 Feb-27-2021, 08:28 AM
Last Post: perfringo
  Invalid syntax error - need help fixing calgk01 3 316 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Why do I get Syntax error? TechNitium 1 247 Jan-22-2021, 12:49 PM
Last Post: snippsat
  syntax error CompleteNewb 8 502 Jan-08-2021, 04:05 PM
Last Post: deanhystad
  Syntax Error: Outside a function? aplusfordao2702 2 302 Dec-01-2020, 03:25 PM
Last Post: deanhystad
  Error on nested loop : Invalid syntax dvazquezgu 3 403 Nov-25-2020, 10:04 AM
Last Post: palladium
  return out of loops syntax error felixf 7 426 Nov-03-2020, 01:00 PM
Last Post: perfringo
  sqlite3.OperationalError: near "=": syntax error Maryan 1 916 Oct-31-2020, 12:09 AM
Last Post: Maryan
  Strange syntax error with f-strings Askic 6 661 Oct-16-2020, 10:40 AM
Last Post: Askic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020