pyhton---简单的价格和目录格式输出

原创
2012/03/11 10:50
阅读数 21.4W
width = input('Please enter width: ')
price_width = 10
item_width = width - price_width
header_format = '%-*s%*s'
format = '%-*s%*.2f'

print '=' * width

print header_format % (item_width,'Item',price_width,'Price')

print format % (item_width, 'Apples',price_width,0.4)
print format % (item_width, 'Pears', price_width,0.5)
print format % (item_width, 'Cantaloupes', price_width,1.92)
print format % (item_width, 'Dried Apricots (16 oz.)', price_width,8)
print format % (item_width, 'Prunes (4 lbs.)', price_width,12)
print '=' * width

 

使用*符号可以从元组中读取字段宽度

python version 2.4.3

 

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部