为什么这个def函数没有在Python中执行?
当我输入Zed Shaw练习18中的下面一段代码时,Python只是在弹出另一个提示为什么这个def函数没有在Python中执行?,python,function,Python,Function,当我输入Zed Shaw练习18中的下面一段代码时,Python只是在弹出另一个提示 # this one is like our scripts with argv def print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1, arg2) # ok, that *args is actually pointless, we can just do this def print_two_a
# this one is like our scripts with argv
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" % (arg1, arg2)
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2) :
print "arg1: %r, arg2: %r" % (arg1, arg2)
# this just takes one argument
def print_one(arg1) :
print "arg1: %r" % arg1
# this one takes no argument
def print_none() :
print "I got nothin'."
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
最后四行的缩进是错误的。因为它们是缩进的,python解释器认为它们是print_none的一部分。取消提示,口译员将按预期呼叫他们。应该是这样的:
>>> print_two("Zed","Shaw")
[... etc ...]
最后四行的缩进是错误的。因为它们是缩进的,python解释器认为它们是print_none的一部分。取消提示,口译员将按预期呼叫他们。应该是这样的:
>>> print_two("Zed","Shaw")
[... etc ...]
def定义了一个函数。函数是潜在的…它们是一组等待执行的步骤。 要在python中执行函数,必须对其进行定义和调用
def定义了一个函数。函数是潜在的…它们是一组等待执行的步骤。 要在python中执行函数,必须对其进行定义和调用
删除最后一行上的缩进。因为它们是缩进的,所以它们是print_none的一部分,而不是在全局范围内执行。一旦它们回到全局范围,您应该看到它们正在运行。删除最后一行上的缩进。因为它们是缩进的,所以它们是print_none的一部分,而不是在全局范围内执行。一旦它们回到全局范围,您应该看到它们正在运行。您需要保持代码对齐。对上述方法的调用被视为函数print_none的一部分 试试这个:
# this one is like our scripts with argv
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" % (arg1, arg2)
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2) :
print "arg1: %r, arg2: %r" % (arg1, arg2)
# this just takes one argument
def print_one(arg1) :
print "arg1: %r" % arg1
# this one takes no argument
def print_none() :
print "I got nothin'."
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
您需要使代码保持一致。对上述方法的调用被视为函数print_none的一部分 试试这个:
# this one is like our scripts with argv
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" % (arg1, arg2)
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2) :
print "arg1: %r, arg2: %r" % (arg1, arg2)
# this just takes one argument
def print_one(arg1) :
print "arg1: %r" % arg1
# this one takes no argument
def print_none() :
print "I got nothin'."
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
对不起,哪段代码?你在Python REPL中输入了所有这些吗?@senderle对此表示歉意!谢谢你的提醒!对不起,哪段代码?你在Python REPL中输入了所有这些吗?@senderle对此表示歉意!谢谢你的提醒!