这是我第一天学习编程。我正在学习Python编程:John Zelle的《计算机科学导论》第二版,到目前为止一切进展顺利。
唯一的问题是,当我尝试导入一个保存的程序时,我会得到一个syntaxerror。我编写程序并在执行之前保存它,但是当我尝试导入它时,我得到了错误。我试着打开一个新的贝壳,但没有雪茄。我使用的是OSXLion10.8和Python2.7.3。如有任何帮助,我们将不胜感激。问题是这样的:
>>> #File: chaos.py
>>> #A simple program illustrating chaotic behavior.
>>> def main():
print "This program illustrates a chaotic function"
x=input("Enter a number between 0 and 1: ")
for i in range(10):
x = 3.9 * x * (1-x)
print x
>>> main()
This program illustrates a chaotic function
Enter a number between 0 and 1: .25
0.73125
0.76644140625
0.698135010439
0.82189581879
0.570894019197
0.955398748364
0.166186721954
0.540417912062
0.9686289303
0.118509010176
>>> import chaos
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
import chaos
File "chaos.py", line 1
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
^
SyntaxError: invalid syntax
我猜你是在把终端的内容一字不差地复制到文件中。还有很多不应该出现的东西,包括版本提示。
文件应该有如下内容:
没有
>>>
,没有...
,没有制表符,当然也不会复制版本信息:看起来您的
chaos.py
脚本的第一行有一行不是python:应该通过以
#
符号开头的行来删除或注释掉它。需要记住的一些技巧:
IndentationError
s相关问题 更多 >
编程相关推荐