python - 格式化字符串输出以避免空格
问题描述
我需要修改这段代码以消除它$
和它后面的数量之间的空间。这是当前的输出:
Choose a color to paint the wall:
Cost of purchasing red paint: $ 35
Choose a color to paint the wall:
Cost of purchasing blue paint: $ 75
这就是我想要的样子:
Choose a color to paint the wall:
Cost of purchasing red paint: $35
Choose a color to paint the wall:
Cost of purchasing blue paint: $75
我的代码如下:
import math
# Dictionary of paint colors and cost per gallon
paintColors = {
'red': 35,
'blue': 25,
'green': 23
}
wallHeight = float(input('Enter wall height (feet):\n'))
wallwidth = float(input('Enter wall width (feet):\n'))
wallArea = float(wallHeight * wallwidth)
print('Wall area:', int(wallArea), 'square feet')
paintNeeded = wallArea / 400
print('Paint needed: %f' % paintNeeded, 'gallons')
cansNeeded = math.ceil(paintNeeded)
print('Cans needed:', cansNeeded,'can(s)\n')
print('Choose a color to paint the wall:')
paintColor = input()
paintcost = {'red':35,'blue':75}
print('Cost of purchasing', paintColor, 'paint: $', paintcost[paintColor])
解决方案
将最后一行替换为
print(f'Cost of purchasing {paintColor} paint: ${paintcost[paintColor]}')
推荐阅读
- javascript - JS:如何在不不断发出请求的情况下更新来自端点的数量?
- python - 缺少 2 个口味和勺子所需的位置参数?
- .net - 拥有大量大型对象且您永远不想水合的实体 - 推荐的模式是什么?
- regex - 正则表达式搜索字符串的实例然后删除某些字符
- function - 函数有没有办法根据输入返回整数或字符串?
- android - 如何在工具栏中的 SearchView 中处理单击返回箭头
- javascript - 导航回屏幕时反应原生元素按钮组按钮消失
- reactjs - 有没有办法根据 keydown 事件以编程方式触发自动完成?
- graph - 如何在 Stata 中为多个时间段创建不同活动的“堆叠条形图”?
- python - 通过 RTSP 使用相机进行 YOLOv3 对象检测