鱼C论坛

 找回密码
 立即注册
查看: 2616|回复: 6

求该段代码的作用!

[复制链接]
发表于 2021-5-31 17:36:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题

  1. width = int(input('Please enter width:'))

  2. price_width = 10
  3. item_width = width - price_width

  4. header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, price_width)
  5. fmt = '{{:{}}}{{:>{}.2f'.format((item_width, price_width))
  6. print('=' * width)

  7. print(header_fmt.format('Item', 'Price'))

  8. print('-' * width)

  9. print(fmt.format('Apples', 0.4))
  10. print(fmt.format('Pear', 0.5))
  11. print(fmt.format('Cantaloupes', 1.92))
  12. print(fmt.format('Dried Apricots (16 oz.)', 8))
  13. print(fmt.format('Prunes (4 1bs.)', 12))

  14. print('=' * width)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-31 17:56:07 From FishC Mobile | 显示全部楼层
自己运行一下不就OK
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-2 01:22:16 | 显示全部楼层
wp231957 发表于 2021-5-31 17:56
自己运行一下不就OK

输入格式不对,没有结果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-2 09:39:34 | 显示全部楼层
废物弟弟云小舟 发表于 2021-6-2 01:22
输入格式不对,没有结果

你输入整数就行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-2 10:38:53 | 显示全部楼层


输入大于 10 的数值就行:

  1. width = int(input('Please enter width:'))

  2. price_width = 10
  3. item_width = width - price_width

  4. header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, price_width)
  5. fmt = '{{:{}}}{{:>{}.2f}}'.format(item_width, price_width)

  6. print('=' * width)

  7. print(header_fmt.format('Item', 'Price'))

  8. print('-' * width)

  9. print(fmt.format('Apples', 0.4))
  10. print(fmt.format('Pear', 0.5))
  11. print(fmt.format('Cantaloupes', 1.92))
  12. print(fmt.format('Dried Apricots (16 oz.)', 8))
  13. print(fmt.format('Prunes (4 1bs.)', 12))

  14. print('=' * width)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-2 10:42:24 | 显示全部楼层
废物弟弟云小舟 发表于 2021-6-2 01:22
输入格式不对,没有结果

  1. width = int(input('Please enter width:'))

  2. price_width = 10
  3. item_width = width - price_width

  4. header_fmt = '{{:{0}}}{{:>{1}}}'.format(item_width, price_width)
  5. fmt = '{{:{0}}}{{:>{1}.2f}}'.format(item_width, price_width)
  6. print('=' * width)

  7. print(header_fmt.format('Item', 'Price'))

  8. print('-' * width)

  9. print(fmt.format('Apples', 0.4))
  10. print(fmt.format('Pear', 0.5))
  11. print(fmt.format('Cantaloupes', 1.92))
  12. print(fmt.format('Dried Apricots (16 oz.)', 8))
  13. print(fmt.format('Prunes (4 1bs.)', 12))

  14. print('=' * width)


  15. '''
  16.     Please enter width:40
  17.     ========================================
  18.     Item                               Price
  19.     ----------------------------------------
  20.     Apples                              0.40
  21.     Pear                                0.50
  22.     Cantaloupes                         1.92
  23.     Dried Apricots (16 oz.)             8.00
  24.     Prunes (4 1bs.)                    12.00
  25.     ========================================
  26. '''
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-2 13:43:32 | 显示全部楼层
先去菜鸟教程看看 str.format()的基本用法,然后理解 "{{"在这里的用处,就能明白了

  1. # "{{ " 在format中的作用
  2. print("{{ {} }}".format("<--- 保留一个'{' --->"))
复制代码

  1. # 先取到值(可用索引),然后在冒号后设定填充格式:{索引:[填充字符][对齐方式][宽度]}
  2. # *<20:左对齐,总共20个字符,不够的用*号填充
  3. print('{0:*<20}'.format('hellopython'))
  4. # *>20:右对齐,总共20个字符,不够的用*号填充
  5. print('{0:*>20}'.format('hellopython'))
  6. # *^20:居中显示,总共20个字符,不够的用*号填充
  7. print('{0:*^20}'.format('hellopython'))

  8. 输出:
  9. hellopython*********
  10. *********hellopython
  11. ****hellopython*****
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 05:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表