Tag Archives: not all arguments converted during string formatting

Python TypeError: not all arguments converted during string formatting

Where?

Run the python program, and the error will appear on the line return “unknown object of% s”% value

Why?

% s means to replace the value variable with a string. However, the value is a python tuple. If the tuple cannot be directly formatted by% s and%, an error will be reported

Way?

Use format or format_ Map format string instead of%

error code

def use_type(value):
    if type(value) == int:
        return "int"
    elif type(value) == float:
        return "float"
    else:
        return "Unknow Object of %s" % value

if __name__ == '__main__':
    print(use_type(10))
    # The tuple argument is passed
    print(use_type((1, 3)))

Correcting the code

def use_type(value):
    if type(value) == int:
        return "int"
    elif type(value) == float:
        return "float"
    else:
        # format method
        return "Unknow Object of {value}".format(value=value)
        # format_map method
        # return "Unknow Object of {value}".format_map({
        #     "value": value
        # })


if __name__ == '__main__':
    print(use_type(10))
    # Passing tuple parameters
    print(use_type((1, 3)))