为什么这个def函数没有在Python中执行? [英] Why is this def function not being executed in Python?

查看:1304
本文介绍了为什么这个def函数没有在Python中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #这个Python只是在我从Zed Shaw练习18中输入以下代码时提出另一个提示。一个是像我们的脚本与argv 
def print_two(* args):
arg1,arg2 = args
printarg1:%r,arg2:%r%(arg1,arg2)

#好的,那* args实际上没有意义,我们可以这样做
def print_two_again(arg1,arg2):
printarg1:%r,arg2:%r %(arg1,arg2)

#这只需要一个参数
def print_one(arg1):
printarg1:%r%arg1

#这一个没有参数
def print_none():
print我什么也没有。

$ b print_two(Zed,Shaw)
print_two_again(Zed,Shaw)
print_one(First!)
print_none()


解决方案

最后四行的缩进是错的。因为它们是缩进的,python解释器认为它们是 print_none()的一部分。取消他们的意图,口译员会按照预期给他们打电话。它应该是这样的:

 >>> print_two(Zed,Shaw)
[... etc ...]


Python is simply bringing up another prompt when I enter the following piece of code from Zed Shaw exercise 18.

# 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()

解决方案

The indentation of the last four lines is wrong. Because they're indented, the python interpreter thinks they're part of print_none(). Unindent them, and the interpreter will call them as expected. It should look like this:

>>> print_two("Zed","Shaw")
[... etc ...]

这篇关于为什么这个def函数没有在Python中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆