2013-06-18 57 views
1

我已经为python生成了以下一段代码,但出于某种原因,我无法弄清楚为什么它返回错误:Python 2.7 TypeError:不支持的操作数类型为%:'NoneType'和'元组

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple

我已经在看,但我不能看到我做错了什么:S

temp_appended_data = [] 

def runme(): 
    global temp_appended_data 

    def test(): 
     return "testdata" 

    def no(): 
     return "22453.32214" 

    def time(): 
     return "22:12" 

    def name(): 
     return "george" 

    temp_appended_data.append("""test example <br> 
           Test: % <br> 
           no: % <br> 
           time: % <br> 
           name: % <br> 
           """) % (test(),no(),time(),name()) 

    print temp_appended_data 

runme() 

任何人都能够看到和解决我做了什么错?

感谢 - HYFLEX

回答

5

list.append回报None。您可能想要移动括号,以便格式化一个字符串,然后将它们传递给append而不是附加未格式化的字符串,然后尝试格式化None

temp_appended_data.append("""test example <br> 
          Test: %s <br> 
          no: %s <br> 
          time: %s <br> 
          name: %s <br> 
          """ % (test(),no(),time(),name())) 

另外,%不是有效的替换字段。您可能打算使用%s

+1

,还可以使用'%s'as格式规范 – Ber

+0

@Ber - 右。我没有注意到那个。我只是解决了回溯中的第一个问题。 :) – mgilson

+0

我想最后'%s'应该是''%格式化操作... – Bakuriu

相关问题