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
Recommended Tutorials:
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__":
Recommended Tutorials:
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.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,007 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,135 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,249 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,777 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,287 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,894 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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