【发布时间】:2016-12-01 00:28:56
【问题描述】:
我尝试在 mac 终端上运行多个 python 函数,对于这个程序,它们都返回语法错误
def spam():
print "R"
spam()
它返回了错误:
./test.py: line 1: syntax error near unexpected token `('
./test.py: line 1: `def spam():'
这确实是我能找到的最简单的功能。
需要明确的是,终端正在运行程序的其余部分,但它不能处理函数。
#!/usr/bin/python
import math
number = int(raw_input("What's your surd?"))
print type(number)
#Just to let us know what the input is
if type(number) == int:
print "Number is an integer"
else:
print "Please enter a number"
value = math.sqrt(number)
#Takes the number and square roots it
new_value = int(value)
#Turns square root of number into an integer
if type(new_value) == int:
print "Surd can be simplified"
print new_value
else:
print "Surd cannot be simplified"
print value
这个程序运行良好,即使它现在有点错误,但下面的程序返回与前一个函数相同的错误。
# define a function
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = int(input("What's your number? "))
print_factors(num)
为什么终端在没有语法错误的情况下返回语法错误?
【问题讨论】:
-
你是如何运行你的python程序的?试试
python ./test.py。 -
您收到的错误消息表明
bashshell 正在尝试解释该文件。如果您在 shell 中键入def spam():,您将收到相同的消息。这意味着该文件不包含正确的 shebang 行(如您在较长摘录中显示的#!/usr/bin/python)。
标签: python macos function terminal