2024-10-08 18:30:33 发布
网友
我需要把我的句子放在一行上,但我不知道怎么说。我尝试了.string(),但不确定是否正确使用了它。 我的代码是:
def printCurrency(value): print("$" + format(value, '.2f')) print("That is" end=" ") printCurrency(cost) print("for this service")
它是这样打印出来的
那是22美元
为了这项服务。你知道吗
谢谢
只要把printCurrency改成formatCurrenty就可以了
printCurrency
formatCurrenty
def formatCurrency(value): return "$" + format(value, '.2f') # don't print the value but return it print("That is %s for this service" % formatCurrenty(cost))
对于这个特定问题,我将使用formatCurrency解决方案。如果你的问题比较笼统:
要在python2.x中使用print而不获取换行符,请在末尾添加逗号。你知道吗
print
例如
>>> def f(): ... print 'hello', ... print 'world' ... >>> f() hello world
在python3.x中,只需设置end='':
end=''
>>> def f(): ... print('hello ', end='') ... print('world') ... >>> f() hello world
只要把
printCurrency
改成formatCurrenty
就可以了对于这个特定问题,我将使用formatCurrency解决方案。如果你的问题比较笼统:
要在python2.x中使用
print
而不获取换行符,请在末尾添加逗号。你知道吗例如
在python3.x中,只需设置
end=''
:相关问题 更多 >
编程相关推荐