Syntax error in basic file

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1235 Views - Last Post: 29 August 2015 - 10:51 AM Rate Topic: -----

#1 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Syntax error in basic file

Posted 29 August 2015 - 08:46 AM

I am trying to run a basic program in the John Zelle Intro to Python textbook but get a Syntax error message:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> # File: chaos.py
>>> # A simple program illustrating chaotic behavior
>>> 
>>> def main():
	print("This program illustrates a chaotic function")
	x=eval(input("Enter a number between 0 and 1: "))
	for i in range(10):
		x = 3.9 * x * (1-x)
		print(x)

		
>>> main()


I have typed this exactly as it is given in the textbook and do not understand what is incorrect. I would greatly appreciate assistance. I have a homework assignment due tonight and I can't even get a basic example to run! :sweatdrop:/>

Thanks in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Syntax error in basic file

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 08:48 AM

Post the error message in full. It isn't random text, it tells you what the problem is and on what line it occurred.
Was This Post Helpful? 0
  • +
  • -

#3 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 08:56 AM

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
 Type "copyright", "credits" or "license()" for more information.
 >>> import chaos
 Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     import chaos
   File "C:\Python34\chaos.py", line 1
     Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
              ^
 SyntaxError: invalid syntax
 >>> 

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 09:07 AM

Did you type that all exactly as posted, into a file, including the "Python 3.4.3 (v3.4.3.." stuff?

The prompt >>> indicates that the code is to be entered and executed at the Python console, or command window. Look for "Python Interactive Shell" on your computer.

If you look further back in your book I'm sure the author will at some point have given instructions as to how, and where, to enter and run code samples.

You should also use float() in preference to eval().

When I run the Python Interactive Shell it first looks like this:

Attached Image

Quote

I have a homework assignment due tonight and I can't even get a basic example to run!

It seems that you have skipped quite an amount from your book if this is the first example you've attempted to run.

This post has been edited by andrewsw: 29 August 2015 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 09:24 AM

My interactive shell looks similar except it is 3.4.3 and no, I didn't type the first lines. Those are the first lines in the shell when I open it. I have followed the steps in this book for downloading and installing the program exactly and am getting nowhere. I could get a VERY basic example
print("Hello, World!")  input("Press <Enter>")
to work using Notepad to type the code and then open the shell and import the file. This one will not work that way either. I have asked my professor and all he told me was that is looked as if I had the file in the wrong directory. I accepted all defaults and the path is C:\Python34\chaos.py. I also changed eval to float as suggested but this did not make a difference.

I haven't skipped anything in the textbook. I used the reference section in the back to find out how to download the program and install, then ran the simple "Hello, World" file with no trouble. When I started work in the 1st chapter, I have gotten to this point and am stymied.

Thanks for your suggestions.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 09:26 AM

Quote

File "C:\Python34\chaos.py", line 1

It looks like it is finding the file, so what is on line 1 of the file?
Was This Post Helpful? 0
  • +
  • -

#7 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 09:47 AM

I am not certain what it is referring to. Line 1 has the default language when you open the interactive shell. The fist line of code is
def main():
. Here's the way it is written in the book and how I typed it into IDLE (with the exception of using "float" instead of "eval"):

# File: chaos.py
# A simple program illustrating chaotic behavior
 
def main():
print("This program illustrates a chaotic function")
	x=float(input("Enter a number between 0 and 1: "))
	for i in range(10):
		x = 3.9 * x * (1 - x)
		print(x)

		
main()


Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 09:57 AM

Quote

Line 1 has the default language when you open the interactive shell. The fist line of code is

This is a mixed message. What is the very first line at the top of the file chaos.py?
Was This Post Helpful? 0
  • +
  • -

#9 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 09:59 AM

Is it possible that my problems could be related to the version of Python on my Win 8.1 64-bit computer?

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 10:04 AM

It is very unlikely to be anything to do with the version, it is more likely what I am trying to establish, whether you have text like this

Quote

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32

at the beginning of your file, which shouldn't be there.

This post has been edited by andrewsw: 29 August 2015 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

#11 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 10:10 AM

Yes. I will try removing this. But that's a bit confusing since your shell shows a similar line.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 10:14 AM

Yes, the text appears in the Shell, but shouldn't in a Python file. The Shell is just letting you know that it's working and what version of Python it's using.

This post has been edited by andrewsw: 29 August 2015 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#13 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 10:21 AM

okay. I am on the same "page" now, I think. The first line of code is
>>> def main():

There are two lines of comments above that giving the name of the file and the description (purpose) of the program.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Syntax error in basic file

Posted 29 August 2015 - 10:37 AM

In a file you shouldn't have >>>, that is the prompt that appears when using the shell.

Interestingly, the author himself responded to his use of eval in his book at SO.
Was This Post Helpful? 0
  • +
  • -

#15 pythonoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-August 15

Re: Syntax error in basic file

Posted 29 August 2015 - 10:38 AM

okay. I understand that. but I am not seeing anything other than the shell. I keep getting error messages. I tried "import chaos" again and received the following error message this time:
>>> import chaos.py
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import chaos.py
  File "C:\Python34\chaos.py", line 5
    print("This program illustrates a chaotic function")
        ^
Indentationerror: expected an indented block


I assume this is from trying to go back to using Notepad to type the code and then save as 'chaos.py'
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2