gpt4 book ai didi

python - 如何在 python 中的字符串(句子)中打印函数返回的字典输出? (加工)

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:39 24 4
gpt4 key购买 nike

我正在创建一个程序,通过采用以下输入来计算一顿饭的总费用:meal_cost,tax_rate,tip_rate,number_eating

并通过函数调用将它们打印在字符串中。我查看了 StackOverflow,但找不到适合我情况的问题(打印从字符串中的函数返回的字典)

我有一个函数接受所有输入并返回一个字典输出。我想通过函数调用将这些返回值打印在一个字符串中,全部在同一行中。这是我尝试过的:

def calculatedCost(meal_cost,tax_rate,tip_rate,number_eating):
tax = round(float(meal_cost * tax_rate) / 100,2)
tip = round(float(meal_cost * tip_rate) / 100,2)
total_cost = round(meal_cost + tax + tip,2)
division = round(total_cost / number_eating,2)
return {'tax': tax, 'tip': tip, 'total_cost':total_cost, 'division':division}

print("The cost of your meal is: {total_cost}, the tax on your meal is: {tax}, the tip is equal to: {tip}, and the split total is: {division}".format(calculatedCost(62.75,5,20,2)))

我得到这个错误:(我正在使用处理)

KeyError: total_cost
processing.app.SketchException: KeyError: total_cost
at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:240)
at jycessing.mode.run.SketchRunner.lambda$2(SketchRunner.java:119)
at java.lang.Thread.run(Thread.java:748)

最佳答案

你需要解压dict(注意双星号):

print("The cost of your meal is: {total_cost}, the tax on your meal is: {tax}, the tip is equal to: {tip}, and the split total is: {division}".format(**calculatedCost(62.75,5,20,2)))

关于python - 如何在 python 中的字符串(句子)中打印函数返回的字典输出? (加工),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53898340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com