Python 字符串函数问题
尝试创建书目格式函数Python 字符串函数问题,python,string,function,Python,String,Function,尝试创建书目格式函数 def bibformat_mla(author,title,city,publisher,year): return "author. title. city: publisher, year" author = ('Perry') title = ('Personal Identity') city = ('London') publisher = ('University of California Press') y
def bibformat_mla(author,title,city,publisher,year):
return "author. title. city: publisher, year"
author = ('Perry')
title = ('Personal Identity')
city = ('London')
publisher = ('University of California Press')
year = (2008)
print (author + title + city + publisher + year)
我的代码似乎不起作用。帮助?您遇到了一个大问题。您的返回是在您的功能的开始。当return语句脱离函数时,您的代码将永远无法通过return语句 您有两种可能的解决方案 是否将返回更改为打印? 您可能想打印出来,而不是返回: 移动返回? 您可以将其移动到底部,而不是将返回值保持在函数的顶部:
希望这能让你朝着正确的方向前进:
#! /usr/bin/env python
def bibformat_mla(author, title, city, publisher, year):
return '%s. %s. %s: %s, %s' % (author, title, city, publisher, year)
def main():
author = 'Perry'
title = 'Personal Identity'
city = 'London'
publisher = 'University of California Press'
year = 2008
s = bibformat_mla(author, title, city, publisher, year)
print s
if __name__ == '__main__':
main()
那是什么语言。基本的?我假设这是python。您的返回不应该在您的功能结束时吗?当它刚开始的时候,它永远不会超过这一点。我运行的是python 2.7,我试图以作者的格式返回一个输出。标题城市:出版商,请注意,您将立即覆盖所有作者、标题。。。在你的函数中。你如何发布代码?不起作用。“有人能得到我想要做的吗?”DaveKeddy:你可以通过编辑你的问题并在最后粘贴新代码来发布更多的代码,最好是用某种解释。你可能想考虑OP覆盖所有参数的问题。您也可以这样做,从而使所有参数都变得不必要。我始终更喜欢使用新的字符串格式化方法。格式化,因为它可以更清晰,并且是Python DocsGood调用Nick T建议的。我真的应该开始使用它。。。我仍然在使用Python2.6,我有点喜欢C风格的格式化方法。它在Python2.6中,但是你不能太懒,只说{},{}…,你需要说{0},{1}。。。。我想你可以使用文本而不是数字标签作为字段,然后在2.6版本的字典中输入。是的,几天前我还在玩它。不过,它似乎不适用于关键字args或dict,但我可以这样做:将%s打印为%bs%cs%{'c':3,'a':1,'b':2},它将打印1 2 3。
def bibformat_mla(author,title,city,publisher,year):
author = ('Perry')
title = ('Personal Identity')
city = ('London')
publisher = ('University of California Press')
year = (2008)
print (author + title + city + publisher + year)
return "author. title. city: publisher, year"
#! /usr/bin/env python
def bibformat_mla(author, title, city, publisher, year):
return '%s. %s. %s: %s, %s' % (author, title, city, publisher, year)
def main():
author = 'Perry'
title = 'Personal Identity'
city = 'London'
publisher = 'University of California Press'
year = 2008
s = bibformat_mla(author, title, city, publisher, year)
print s
if __name__ == '__main__':
main()