Python 需要了解格式的帮助;。2f“;命令及其在我的代码中不起作用的原因
我正在练习格式语句,但我不太了解它是如何工作的。我特别尝试使用“.2f”命令来理解format命令这是我目前拥有的代码,第二行在返回之前运行,并出现错误:Python 需要了解格式的帮助;。2f“;命令及其在我的代码中不起作用的原因,python,python-3.x,Python,Python 3.x,我正在练习格式语句,但我不太了解它是如何工作的。我特别尝试使用“.2f”命令来理解format命令这是我目前拥有的代码,第二行在返回之前运行,并出现错误: salesTax = 0.08 salesAmount = str(input('Enter the sales amount: ')) print ('The sales amount is', format('$'+salesAmount, '.2f')) print ('The sales tax is', format("$"
salesTax = 0.08
salesAmount = str(input('Enter the sales amount: '))
print ('The sales amount is', format('$'+salesAmount,
'.2f'))
print ('The sales tax is', format("$"+'.2f'))
print('The total sale is', format("$"+totalSales*salesTax,
'.2f'))
input("\nRun complete. Press the Enter key to exit.")
我正在尝试制作一个脚本,该脚本显示以下示例运行的输出:
//Sample run:
Enter the sales amount: 1234
The sales amount is $ 1,234.00
The sales tax is $ 98.72
The total sale is $ 1,332.72
Run complete. Press the Enter key to exit
.2f应在format方法之外。
例如,尝试
print(“{.2f}”.format(12.345678))
.2f应该在format方法之外。
例如,尝试打印(“{.2f}”。格式(12.345678))TL;博士
print('The sales amount is $', format(salesAmount, '.2f'))
细分:
将数字转换为带浮点表示法(f
部分)的格式为2位小数(.2
部分)的字符串
现在您已经有了一个字符串,您可以使用pass to print将其连接起来,也可以使用+
或其他方式连接到以前的代码
'The sales amount is $' + the_formatted_number
TL;博士
细分:
将数字转换为带浮点表示法(f
部分)的格式为2位小数(.2
部分)的字符串
现在您已经有了一个字符串,您可以使用pass to print将其连接起来,也可以使用+
或其他方式连接到以前的代码
'The sales amount is $' + the_formatted_number
是一个内置函数,它接受两个参数:value
和format\u spec
格式(“$”+销售额“.2f”)
将给您一个错误,因为它需要一个数字或数字的字符串表示形式作为值
,以应用您提供的格式。您将得到一个错误:
ValueError:类型为“str”的对象的格式代码“f”未知
格式(销售额,.2f')
这将正常工作
注意:如果您使用的是Python 3.6+,也可以使用f字符串:
f"{12.345678:.2f}"
是一个内置函数,它接受两个参数:value
和format\u spec
格式(“$”+销售额“.2f”)
将给您一个错误,因为它需要一个数字或数字的字符串表示形式作为值
,以应用您提供的格式。您将得到一个错误:
ValueError:类型为“str”的对象的格式代码“f”未知
格式(销售额,.2f')
这将正常工作
注意:如果您使用的是Python 3.6+,也可以使用f字符串:
f"{12.345678:.2f}"
语法是
format(value, format_spec)
当您使用.2f
格式时,值应该是浮点
,而不是字符串,因此您需要将输入转换为浮点
(input()
返回字符串,无需使用str()
。在格式化之前,不能将$
连接到值,需要将其连接到结果
在print()
调用中,还使用了一个不存在的变量totalSales
,它应该是salesAmount
salesTax = 0.08
salesAmount = float(input('Enter the sales amount: '))
print ('The sales amount is', '$' + format(salesAmount, '.2f'))
print ('The sales tax is', '$' + format(salesTax*salesAmount, '.2f'))
print('The total sale is', '$' + format(salesAmount + salesAmount*salesTax, '.2f'))
input("\nRun complete. Press the Enter key to exit.")
这就是说,format()
函数的这种用法现在并不常见。使用f-string或str.format()
更容易,它允许您将格式规范嵌入纯文本字符串中,并将多个值一起格式化,例如
print("The sales amount is ${.2f}".format(salesAmount))
语法是
format(value, format_spec)
当您使用.2f
格式时,值应该是浮点
,而不是字符串,因此您需要将输入转换为浮点
(input()
返回字符串,无需使用str()
。在格式化之前,不能将$
连接到值,需要将其连接到结果
在print()
调用中,还使用了一个不存在的变量totalSales
,它应该是salesAmount
salesTax = 0.08
salesAmount = float(input('Enter the sales amount: '))
print ('The sales amount is', '$' + format(salesAmount, '.2f'))
print ('The sales tax is', '$' + format(salesTax*salesAmount, '.2f'))
print('The total sale is', '$' + format(salesAmount + salesAmount*salesTax, '.2f'))
input("\nRun complete. Press the Enter key to exit.")
这就是说,format()
函数的这种用法现在并不常见。使用f-string或str.format()
更容易,它允许您将格式规范嵌入纯文本字符串中,并将多个值一起格式化,例如
print("The sales amount is ${.2f}".format(salesAmount))
语法是
“formatString”。format(value1,value2,value3,…)
Hmm,还有一个函数format
:format(value,format\u string)
,但它并不常用。语法是“formatString”。format(value1,value2,value3,…)
Hmm,还有一个函数format
:(值、格式和字符串)
,但它不是常用的。这是不正确的。他没有使用format方法,而是使用format函数,该函数需要两个参数,用于单个元素。这是不正确的。他没有使用format方法,而是使用format函数,该函数需要两个参数,用于单个元素。虽然这是可行的,但您没有解释了解format函数的工作原理,这就是requested@lmiguelvargasf感谢您的澄清。在使用format命令时,我会记住这一点。虽然这是可行的,但您没有解释format函数是如何工作的,这就是requested@lmiguelvargasf谢谢你的澄清,我在使用格式化命令。非常感谢,这非常有帮助,我知道这是一个简单的概念,但出于某种原因我不能绕着它转。非常感谢,这非常有帮助,我知道这是一个简单的概念,但出于某种原因我不能绕着它转。哇,谢谢你的精彩和详细的回答,我会记住的nd现在可能会写一个新的脚本来练习str.format()。哇,谢谢你的精彩和详细的回答,我会记住这一点,现在可能还会写一个新的脚本来练习str.format()。