对于我的生活,我似乎无法将这些数字加起来!它们都是单独打印的。例如,它显示"对于..."这将是$ 95.00 $ 90 $ 8.50。我认为这很简单,因为支架编号是正确的......但我似乎无法做到这一点!
print("That is " + str((formatC(pack[packageSelect-1]["adult"]*noAdult) + formatC(pack[packageSelect-1]["child"]*noChild) + (formatC(AREAS.get(delivery))))) + "for " + pack[packageSelect]["name"] + " package for " + str(noAdult) + " " + adult + " and ", str(noChild) + " " + child + ", delivered to " + str(deliveryArea.title()) + ".")
我想要"成人" +"孩子" +"交付"总算..不是单独的价格!
答案 0 :(得分:2)
您正在添加这些数字的字符串表示形式。字符串的+
运算符执行连接。如果您想要添加金额,则必须在float
时添加这些金额,然后将结果转换为字符串。
答案 1 :(得分:1)
您正在添加格式化字符串;添加字符串将它们一个接一个地放置。
首先添加数字:
total = (
pack[packageSelect-1]["adult"] * noAdult +
pack[packageSelect-1]["child"] * noChild +
AREAS.get(delivery))
print("That is " + str(formatC(total)) + "for " + pack[packageSelect]["name"] + " package for " + str(noAdult) + " " + adult + " and ", str(noChild) + " " + child + ", delivered to " + str(deliveryArea.title()) + ".")
您可能想要研究如何使用str.format()
格式化字符串;它会使您的打印代码更简单,更易读:
print("That is {total} for {pack['name']} package for {noAdult} {adult} and {noChild} {child}, delivered to {area}.".format(
total=formatC(total), pack=pack[packageSelect], noAdult=noAdult, adult=adult,
noChild=noChild, child=child, area=deliveryArea.title()))