Python Forum
Only one of two lines print/syntax error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Only one of two lines print/syntax error
#1
I searched the internet for syntax errors but I just get a general definition of what a syntax error could be. I am new to Python and teaching myself via online lessons and meet ups in my area. I have this lesson that should print 2 simple lines which are lines 4 and 9 but only line 4 prints and with a syntax error it looks like and Line 9 does not even appear. Line 9 ( print "This should run.") is a three letter sentence and it is spelled correctly and the spaces are correct and I even retyped it three times. I typed the missing Line 9 code in a code validator that I found online and the line of code was validated as being correct. I use Atom as my editor. Below is what I enter in Atom and I will also show what the results are in Terminal. I've tried self help to see why only line 4 prints but cannot figure out why line 9 will not print if it is typed correctly.  I've read this code backwards and forwards but it doesn't come out right. I typed it slow and checked the spacing.

This is what I type in Atom

# A comment, this is so you can read your program later.

# Anything after the # is ignored by python.

print "I could have code like this." and the comma after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."

print "This should run."

This is what I get in Terminal

Renays-MacBook-Pro:mystuff renayjohnson$ cd thestuffa

Renays-MacBook-Pro:thestuffa renayjohnson$ python ex2.py
  File "ex2.py", line 4
    print "I could have code like this." and the comma after is ignored
                                                     ^
SyntaxError: invalid syntax
Renays-MacBook-Pro:thestuffa renayjohnson$ 
Reply
#2
Welcome to the forum. Take your time to read the forum rules. In the future, please use CODE tags around your code

print "I could have code like this." and the comma after is ignored
the part after the closing double-quote is the problem - I assume that's something from your book/tutorial explanations


print "I could have code like this."
Reply
#3
You print what is in the string wrapped in "". All that part ( and the comma after is ignored ) is like you want to print a bunch of variables named and, the, comma, after, is, ignored. You can do that but you have to separate them with a comma. So the right syntax will be:

print "I could have code like this.", and, the, comma, after, is, ignored
"And", "is" are key words in Python.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,188 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 390 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,608 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,233 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  print(data) is suddenly invalid syntax db042190 6 1,204 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  syntax error question - string mgallotti 5 1,319 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,262 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 896 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,860 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,368 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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