嘿,所以我要通过学校使用的代码作业网站提交代码,由于某种原因,这表示我的行距与金额之间存在问题,如下面的屏幕截图所示,左边的输出是预期的,右边的输出是我得到的。我执行此操作的代码有什么问题?同样在每个输出的末尾,它还会打印“无”。为什么?
答案 0 :(得分:0)
一个问题是您的 thank_donor 函数返回了 print 函数,然后又在那儿调用了 print 函数。您希望函数仅返回纯字符串,然后可以打印。
答案 1 :(得分:0)
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
条语句即可。
答案 2 :(得分:0)
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}”。format(amount)的末尾使用“,” ,因为这会导致空格,而是使用“ +” 。
"\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")