Maison  >  Questions et réponses  >  le corps du texte

python3中 format方法的一些问题

_manny = int(input('请输入您的预算:')) #预算

_cargo = [['iphone', 5888], ['computer', 6400], ['desk', 800], ['pen', 40], ['smoke', 400]] # 商品清单

_quit = 'q' # 按q退出

_consume = 0 # 消费金额

print('以下是为您准备的商品列表,请按对应序号选择您需要的商品:')

for i in range(4):

print('序号{}','商品名称:{}','商品单价:{}'.format(i+1,_cargo[i][0], _cargo[i][1]))

初学 format方法学的不是很好。这里老是要不了自己想要的结果。请老司机带带。。。

PHP中文网PHP中文网2741 Il y a quelques jours563

répondre à tous(4)je répondrai

  • 阿神

    阿神2017-04-18 10:28:10

    >>> print('序号{},商品名称:{},商品单价:{}'.format(1,2, 3))
    序号1,商品名称:2,商品单价:3

    répondre
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:28:10

    >>> for i in range(4):
            print('序号{}'.format(i+1),'商品名称:{}'.format(_cargo[i][0]),'商品单价:{}'.format( _cargo[i][1]))
    
        
    序号1 商品名称:iphone 商品单价:5888
    序号2 商品名称:computer 商品单价:6400
    序号3 商品名称:desk 商品单价:800
    序号4 商品名称:pen 商品单价:40
    
    
    你要的是这样的效果吗?

    répondre
    0
  • 高洛峰

    高洛峰2017-04-18 10:28:10

    print('序号{0:}','商品名称:{1:}','商品单价:{2:}'.format(i+1,_cargo[i][0], _cargo[i][1]))

    répondre
    0
  • ringa_lee

    ringa_lee2017-04-18 10:28:10

    pour moi dans la plage(4) :

    print('序号{0} 商品名称:{1} 商品单价:{2}'.format(i+1,_cargo[i][0], _cargo[i][1]))

    répondre
    0
  • Annulerrépondre