Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple syntax error help
#1
Im new to programming in general and am playing with the water. I seem to have hit a syntax error that I just cannot figure out why.

person = input('Enter your name: ')
print('Hello', person)
ask = input('What would you like me to do for you today')
if ask : "Tell me a joke"
print('Caroline has a small nostril')
else:
According to the ide(pycharm) the error message looks like this according to line 6:
else:
^
SyntaxError: invalid syntax

Also what is the best way to go about going back to line 3 after the else command, couldnt test it out cause I couldnt get passed it!

Any help is appreciated!
Reply
#2
i'm new myself but i can offer one small correction

if ask=="Tell me a joke":
print('Caroline has a small nostril')

as for asking the question again, you can use a while statement.
something like this:

while True:
ask=input("")
if ask==" ":
break

maybe someone can give a better answer, but that was what i could think of.
Reply
#3
Sometimes error is on previous line(s). In this case - on line 4. But even after you fix it, indentation on line 5 is incorrect and you need something on line 9 (i.e. body of the else clause)

person = input('Enter your name: ')
print('Hello', person)
ask = input('What would you like me to do for you today')
if ask == "Tell me a joke":
    print('Caroline has a small nostril')
else:
    print('Really, Caroline has a small nostril')
Reply
#4
(Aug-02-2017, 08:40 AM)wikedkid Wrote: if ask=="Tell me a joke":
print('Caroline has a small nostril')
while True:
ask=input("")
if ask==" ":
break
I tried this but it says indentation error for break but when i indent it it says it breaks the loop

(Aug-02-2017, 08:41 AM)buran Wrote: Sometimes error is on previous line(s). In this case - on line 4. But even after you fix it, indentation on line 5 is incorrect and you need something on line 9 (i.e. body of the else clause

I originally tried it with the == on line 4 it still says invalid syntax

EDIT: I dunno why but I retried your way and it worked the second time, maybe a typo i didnt notice in my correcting it, what i want the else to do is loop it back to a previous spot in the code like before or after the Enter your name part
Reply
#5
You need to put the part of code that you want to repeat in a loop. try to do it yourself and ask again if you face problem.
Reply


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