1
legth = 6
price = ''
for i in range(legth):
    price += "{}    ".format(i)
print(price)
line2 = ""
board = []
for i in range(legth):
    board.append([" "]*legth)

for k in range(legth):
    # if k == 0:
    #     line2 += "str(k)   +"
    line2 += "board[{}]".format(k)+ "[{}]  + '  |  ' +".format(k)                      
line2 = line2[:-1]


for k in range(legth):
    
    newline = str(k)+ '|' + exec(line2)
    #line2 = line2[:-1]
    
    print(newline + " "*5) # + line2)
    if i < legth:
        print("  +___+___+___+___+___+___+___+___+" )

为什么会返回

  newline = str(k)+ '|' + exec(line2)
TypeError: can only concatenate str (not "NoneType") to str
4

1 回答 1

0
legth = 6
price = ''
for i in range(legth):
    price += "{}    ".format(i)
print(price)
line2 = ""
board = []
for i in range(legth):
    board.append([" "]*legth)

for k in range(legth):
    # if k == 0:
    #     line2 += "str(k)   +"
    line2 += "board[{}]".format(k)+ "[{}]  + '  |  ' +".format(k)                      
line2 = line2[:-1]


for k in range(legth):
    
    newline = str(k)+ '|' + str(eval(line2))
    #line2 = line2[:-1]
    
    print(newline + " "*5) # + line2)
    if i < legth:
        print("  +___+___+___+___+___+___+___+___+" )

我不知道你想做什么,但试试这个

exec 返回 None 但 eval 返回一些东西

于 2020-11-09T06:49:00.683 回答