对齐可迭代:错误“字符串无法从手动字段规范切换到自动字段编号”

所以这是我的代码:


def f(n):



    if n == 0:

        return 1

    else:

        result = 1

        for i in range(1,n+1):

            result = result * i

        return result






def a(n):

    sum = 0

    z = 0

    for i in range(n+1):

        sum += f(i)

        print('{0:<4}! = {1:<4}  no.multi. ={} sum of {}!  = {}   no.multi. = {}'.format(i,f(i),i,i,sum,z))

        z = z + (i+1)



a(19)

我需要得到这个输出:

http://img.mukewang.com/6124acdb000197a014160538.jpg

但相反,我得到了一种金字塔

http://img2.mukewang.com/6124acea0001250413550669.jpg

我试图在括号内格式化,但我总是收到错误:“无法从手动字段规范切换到自动字段编号”提前谢谢


holdtom
浏览 326回答 1
1回答

扬帆大鱼

我声明了变量strong_i,它是iwith 的字符串!,以!在i使用!.def f(n):&nbsp; &nbsp; if n == 0:&nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; result = 1&nbsp; &nbsp; &nbsp; &nbsp; for i in range(1,n+1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = result * i&nbsp; &nbsp; &nbsp; &nbsp; return resultdef a(n):&nbsp; &nbsp; sum = 0&nbsp; &nbsp; z = 0&nbsp; &nbsp; for i in range(n+1):&nbsp; &nbsp; &nbsp; &nbsp; sum += f(i)&nbsp; &nbsp; &nbsp; &nbsp; strong_i = str(i) + '!'&nbsp; &nbsp; &nbsp; &nbsp; print('{0:<4}= {1:<20}&nbsp; no.multi. = {2:<2} sum of {3:<3}&nbsp; = {4:<20}&nbsp; &nbsp;no.multi. = {5}'.format(strong_i,f(i),i,strong_i,sum,z))&nbsp; &nbsp; &nbsp; &nbsp; z = z + (i+1)a(19)输出:0!&nbsp; = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 0&nbsp; sum of 0!&nbsp; &nbsp;= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 01!&nbsp; = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 1&nbsp; sum of 1!&nbsp; &nbsp;= 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 12!&nbsp; = 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 2&nbsp; sum of 2!&nbsp; &nbsp;= 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 33!&nbsp; = 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 3&nbsp; sum of 3!&nbsp; &nbsp;= 10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 64!&nbsp; = 24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 4&nbsp; sum of 4!&nbsp; &nbsp;= 34&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 105!&nbsp; = 120&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 5&nbsp; sum of 5!&nbsp; &nbsp;= 154&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 156!&nbsp; = 720&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 6&nbsp; sum of 6!&nbsp; &nbsp;= 874&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 217!&nbsp; = 5040&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 7&nbsp; sum of 7!&nbsp; &nbsp;= 5914&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 288!&nbsp; = 40320&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 8&nbsp; sum of 8!&nbsp; &nbsp;= 46234&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 369!&nbsp; = 362880&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 9&nbsp; sum of 9!&nbsp; &nbsp;= 409114&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 4510! = 3628800&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 10 sum of 10!&nbsp; = 4037914&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 5511! = 39916800&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 11 sum of 11!&nbsp; = 43954714&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 6612! = 479001600&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 12 sum of 12!&nbsp; = 522956314&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 7813! = 6227020800&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 13 sum of 13!&nbsp; = 6749977114&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 9114! = 87178291200&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 14 sum of 14!&nbsp; = 93928268314&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 10515! = 1307674368000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 15 sum of 15!&nbsp; = 1401602636314&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 12016! = 20922789888000&nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 16 sum of 16!&nbsp; = 22324392524314&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 13617! = 355687428096000&nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 17 sum of 17!&nbsp; = 378011820620314&nbsp; &nbsp; &nbsp; &nbsp; no.multi. = 15318! = 6402373705728000&nbsp; &nbsp; &nbsp; no.multi. = 18 sum of 18!&nbsp; = 6780385526348314&nbsp; &nbsp; &nbsp; &nbsp;no.multi. = 17119! = 121645100408832000&nbsp; &nbsp; no.multi. = 19 sum of 19!&nbsp; = 128425485935180314&nbsp; &nbsp; &nbsp;no.multi. = 190
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python