简体   繁体   English

在代码中格式化不断导致 ValueError: Unknown format code 'f' for object of type 'str'?

[英]Formatting in a code constantly causes the ValueError: Unknown format code 'f' for object of type 'str'?

Okay, so pretty much I've been getting the error that is described in the title.好的,我几乎得到了标题中描述的错误。 I know that it's a format problem with the code, but as far as solving it.我知道这是代码的格式问题,但就解决它而言。 All of the things regarding how to solve this have proven to be quite useless.所有关于如何解决这个问题的事情都被证明是毫无用处的。 Also, this code is for homework that I have if that provides any context on the matter.此外,如果提供有关此事的任何上下文,则此代码用于我的家庭作业。 The contents of the code are based on a bill since the purpose of the program is for a tip calculator.代码的内容基于账单,因为该程序的目的是用于小费计算器。 I hope that provides context for that matter.我希望这为这件事提供了背景。 Also, I am still relatively new with Python.另外,我对 Python 还是比较陌生。

Anyways, to better show what the code is.无论如何,为了更好地展示代码是什么。 Here's the code that is causing this:这是导致此问题的代码:

print("\n\tBill Amount: ${0:.02f}%".format((meal))), "\n\tTip Suggested: ${0:0.2f}%".format((calculatedTip)), "\n\tTotal Bill ${0:.2f}%".format((total))

I've tried changing the "f" after the.02 to a "d" to see if that would help.我尝试将.02 之后的“f”更改为“d”,看看是否有帮助。 I've tried adding parenthesis to see if that would influence the change, but all of these efforts were rather pointless, since the error was displayed regardless of the changes.我尝试添加括号以查看这是否会影响更改,但所有这些努力都毫无意义,因为无论更改如何都会显示错误。

My main question is what can I do to fix this error?我的主要问题是我能做些什么来解决这个错误? Anyways to whoever is reading this.无论如何,对于正在阅读本文的人。 I hope you have an amazing rest of your day cya!我希望你有一个令人惊叹的 rest !

Try this尝试这个

print("\n\tBill Amount: ${0:.02f}%".format(float(meal)), "\n\tTip Suggested: ${0:0.2f}%".format(float(calculatedTip)), "\n\tTotal Bill ${0:.2f}%".format(float(total)))

This will work if all are numbers even if the numbers are in string format.如果所有都是数字,即使数字是字符串格式,这也会起作用。 Otherwise it will throw ValueError .否则会抛出ValueError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:类型为“str”的 object 的未知格式代码“f” - ValueError: Unknown format code 'f' for object of type 'str' ValueError:类型为'str'的对象的未知格式代码'f'-为什么第二次却不是第一次? - ValueError: Unknown format code 'f' for object of type 'str' - why do I get this the second time but not the first time? ValueError:类型为“ str”的对象的未知格式代码“ g” - ValueError: Unknown format code 'g' for object of type 'str' ValueError: 类型“str”的对象的格式代码“e”未知 - ValueError: Unknown format code 'e' for object of type 'str' 类型为'str'的对象的未知格式代码'f'-Folium - Unknown format code 'f' for object of type 'str'- Folium 类型“ str”的对象的未知格式代码“ b” - Unknown format code 'b' for object of type 'str' “str”类型对象的未知格式代码“g” - Unknown format code 'g' for object of type 'str' Rc4 decrypt ValueError:object 类型为“str”的未知格式代码“x” - Rc4 decrypt ValueError: Unknown format code 'x' for object of type 'str' 'unicode'类型对象的未知格式代码'f' - Unknown format code 'f' for object of type 'unicode' 在我的情况下,“str”类型的 object 的“未知格式代码 'f' 是什么意思? - What does "Unknown format code 'f' for object of type 'str' mean in my case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM