简体   繁体   中英

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

I've produced the following bit of code for python but for some reason I can't work out why it's returning the error:

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

I've had a look under but I can't see what I'm doing wrong :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()

Anyone able to see and fix what I've done wrong?

Thanks - Hyflex

list.append returns None . You probably wanted to move your parenthesis so that your formatting a string them passing it to append rather than appending an unformatted string and then trying to format None .

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

Also, % isn't a valid replacement field. You probably meant to use %s .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM