python关于none的问题...

2025-05-07 19:36:32
推荐回答(1个)
回答(1):

print("That will cost", end=" ")
printCurrency(cost)

print 方法在py3里变成了函数,支持end参数设定本次打印结束符号,将其设定为空格或空串就可以让一个print在完成后不换行继续等待下个打印输出;

还有一种方式:编制strCurrency(cost), 将printCurrency(cost)的输出到控制台改编成按输出格式返回字符串

>>> def strCost(cost):
... return str(cost)
...
>>> print("That will cost:", strCost(17.))
That will cost: 17.0
>>>

或者直接编写一个计算多个costs合计的函数:
>>> def sumcosts(*costs):
... return sum(costs)
...
>>> print("That will cost:", sumcosts(100.0, 12.5, 32))
That will cost: 144.5
>>>