Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “的公式;“两个代表”;价格是两件商品总价的20%。提示用户输入每个值并计算结果_Python - Fatal编程技术网

Python “的公式;“两个代表”;价格是两件商品总价的20%。提示用户输入每个值并计算结果

Python “的公式;“两个代表”;价格是两件商品总价的20%。提示用户输入每个值并计算结果,python,Python,以下是输出的外观: 输入价格1:10.0 输入价格2:20.0 “两人一组”的价格是24美元 我输入的代码是: price_one = float(input('Enter price 1: ')) print(price_one) price_two = float(input('Enter price 2: ')) print(price_two) two_for_price = (price_one + price_two)-((price_one + price_two)*(20

以下是输出的外观:

输入价格1:10.0

输入价格2:20.0

“两人一组”的价格是24美元

我输入的代码是:

price_one = float(input('Enter price 1: '))

print(price_one)

price_two = float(input('Enter price 2: '))

print(price_two)

two_for_price = (price_one + price_two)-((price_one + price_two)*(20/100))

print("The 'two-for' price is $",two_for_price) 
(输入值分别为10.0和20.0。)

但我得到的结果是:

Enter price 1: 10.0

Enter price 2: 20.0

The 'two-for' price is $ 24.0
在最后一行中,我需要:

The 'two-for' price is $24.0

请帮帮我

如果我读对了,您只需要从输出中删除一个空格

将最后一行更改为:


print(“two-for”价格为${0}”。format(two-u-for-u-price))

您的根本问题是,给定一个项目列表,
print
函数行为是打印每个项目,并用空格分隔。这对于快速和脏的打印输出通常很方便,但您需要更精细的东西

您需要做的是创建一个具有适当间距的字符串,然后打印该字符串

所以你可以这样做:

print("The 'two-for' price is $" + str(two_for_price) + ".")
问题是(a)这有点笨拙和不可读,(b)格式不正确,它是“$2.6”而不是“$2.60”

您可以使用Python提供的两种格式化机制中的任何一种,无论是显式的,如下所示:

print("The 'two-for' price is ${0}".format(two_for_price))
或者像这样含蓄的

print("The 'two-for' price is $%f" % two_for_price)
它们看起来都好一点,但格式错误是相同的和更糟的($2.600000!)。幸运的是,两者都提供了很好的自定义格式:

print("The 'two-for' price is ${0:.2f}".format(two_for_price))


两者看起来都相当干净,显示也非常完美。

查找string.format()并不意味着他显然在尝试。。。答案是使用字符串格式
print(“两个代表”的价格是$%0.2f“%两个代表”的价格)
print("The 'two-for' price is $%0.2f" % two_for_price)