嘿,所以我通过我学校使用的代码作业网站提交我的代码,由于某种原因,它说我的行间距与金额有关,这可以在下面的截图中看到,输出在左边是预期的,右边的是我得到的。我的代码有什么问题呢?同样在我的每个输出结束时,它也会打印“无”。为什么?
一个问题是你的thank_donor函数返回print函数,然后你再次调用print函数。您希望该函数只返回您可以打印的纯字符串。
def thank_donor(first_name, last_name, amount, donor_status):
"""prints thank you note with variables in"""
last = last_name.upper()
first = first_name.capitalize()
print(
"----------------------------------------" +
"\n" +
"Note to donor:", last + ",", first +
"\n" +
"----------------------------------------" +
"\n" +
"Dear", first + "," +
在这里,不要在“{:.2f}”。格式(金额)的末尾使用“,”因为它会导致空格,而是使用“+”。
"\n" +
"Thank you for your donation of", "$" + "{:.2f}".format(amount) +
"\n" +
"to our album campaign." +
"\n" +
"This makes you a", donor_status, "member."
"\n" +
"ROCK ON," +
"\n" +
"Blink 992" +
"\n" +
"========================================")
在功能和调用功能时,不要多次使用打印功能。
thank_donor("joe", "bloggs", 100, "Bronze")
def thank_donor(first_name, last_name, amount, donor_status):
"""prints thank you note with variables in"""
last = last_name.upper()
first = first_name.capitalize()
return print(
"----------------------------------------" +
"\n" +
"Note to donor:", last + ",", first +
"\n" +
"----------------------------------------" +
"\n" +
"Dear", first + "," +
"\n" +
"Thank you for your donation of", "$" + "{:.2f}".format(amount),
"\n" +
"to our album campaign." +
"\n" +
"This makes you a", donor_status, "member."
"\n" +
"ROCK ON," +
"\n" +
"Blink 992" +
"\n" +
"========================================")
thank_donor("joe", "bloggs", 100, "Bronze")
试试这个代码。问题是你最后打印none
。发生这种情况是因为你将print
函数返回到另一个print
函数,即print(print(#something#)
。只需删除任何print
语句。