Hello,

I am new to the Python language and I am having difficulty with the print statement. For example, when I type print 4, or print "Hello", i get an error. The error message is:

SyntaxError: invalid syntax (<pyshell#2>, line 1)

When I use python on other computers, the print statement works perfect. The version of Python that I am using that is generating the errors is 3.0.1(GUI). Any help is appreciated.

-Thanks

Recommended Answers

All 9 Replies

Member Avatar for leegeorg07

well thats quite a simple problem

I assume that you're using an old tutorial and thats where the problem is.
before python3 you could just use:

print "hello"

but in python 3 print is now a function so you use:

print("hello")

Excellent, thank you. I have another question, which my be because I am using this old tutorial. In the tutorial it shows the following:

print "First Line."
threeLines()
print "Second Line."

Then it shows the output of the program as:

First Line.


Second Line.

My question is, why when I try the example, I have to do each line one by one and not all together. I have to type a print statement, wait for an output, then type the next and so on.

Thank you.

Are you using the Python Shell (>>> prompts), or an editor like the IDLE editor?

This maybe?:

print("First Line")
input()
print()  # Blank line 1
input()
print()  # Blank line 2
input()
print()  # Blank line 3
input()
print("Second Line")

Each input() just waits for you to press enter before continuing.

Are you using the Python Shell (>>> prompts), or an editor like the IDLE editor?

I think there using IDLE because they said (GUI), and the program IDLE in startup is called "IDLE (Python GUI)".

Well the book that I have is called "How to think like a Computer Scientist: Learning with Python" Printed in 2002. The version of Python on my computer is Python 3.0 IDLE(GUI). What version would you say the example that I posted from the book is in?

Thank you

My goal is to be able to follow and repeat the examples just like the book is showing. Thanks for the help.

Well the book that I have is called "How to think like a Computer Scientist: Learning with Python" Printed in 2002. The version of Python on my computer is Python 3.0 IDLE(GUI). What version would you say the example that I posted from the book is in?

Thank you

If you want to follow the examples in that tutorial, then you may be better off using Python2.5. Python3.0 does make quite a number of significant changes to the syntax. If you have a Windows computer, you can download portable python version 2.5 and install it on a USB flash drive, an inexpensive 256M drive will do.

Portable Python is free from:
http://www.portablepython.com/releases/

Portable Python 1.1 based on Python 2.5.4 installs:
Python 2.5.4
Django-1.0.2-final (for web development)
IPython-0.9.1
Matplotlib-0.98.5.2 ( for graphic plotting)
Numpy-1.2.1 (for high speed arrays)
PIL-1.1.6 (for image manipulation)
Py2exe-0.6.9 (packed python code to .exe files)
PyGame-1.8.1 (the python game toolkit)
PyReadline-1.5
PyScripter v1.9.9.6 (a very nice IDE to write programs with)
PyWin32-212 (window extension library)
Rpyc-2.60 (remote python)
Scipy-0.7.0b1 (a high speed scientific toolkit)
SPE-0.8.4.c (another very nice IDE)
VPython-3.2.11 (for 3D scientific modeling)
wxPython-unicode-2.8.9.1 (a popular GUI toolkit)

Excellent, thank you. I have another question, which my be because I am using this old tutorial. In the tutorial it shows the following:

print "First Line."
threeLines()
print "Second Line."

Then it shows the output of the program as:

First Line.


Second Line.

My question is, why when I try the example, I have to do each line one by one and not all together. I have to type a print statement, wait for an output, then type the next and so on.

Thank you.

If you use the IDLE or SPE editor to run this program from, it will work as advertised ...

def threeLines():
    print
    print
    print

print "First Line."
threeLines()
print "Second Line."

"""
my Python25 output -->
First Line.



Second Line.
"""

For programming using an editor see also:
http://www.daniweb.com/forums/post104834-1.html

Python 2.5 works perfect. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.