我试着MAC终端,他们都返回语法错误上运行多个Python函数,对这一计划的我的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
这个程序运行良好,即使它现在有点bug,但是下面的程序返回与上一个函数相同的错误。
# 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'。 –
您收到的错误消息表明'bash'外壳试图解释该文件。如果您在shell中键入'def spam():',您将得到一个相同的消息。这意味着该文件不包含正确的shebang行(就像您在更长的摘录中显示的'#!/ usr/bin/python')。 – kindall