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)))
Similar Posts:
- Python: __ new__ Method and the processing of typeerror: object() takes no parameters
- [Solved] IllegalArgumentException: object is not an instance of declaring class
- AttributeError: ‘tuple’ object has no attribute ‘extend’
- TypeError: Object of type ‘int32’ is not JSON serializable
- [Solved] TypeError: string indices must be integers, not str
- Solve Python MySQL insert int data error: typeerror% d format: a number is required, not str
- Error reporting and resolution of Python 3 using binascii method
- [412]TypeError: ‘tuple’ object does not support item assignment
- Python: `if not x:` VS `if x is not None:` VS `if not x is None:`
- Python Standard Library: functools (cmp_to_key, lru_cache, total_ordering, partial, partialmethod, reduce, sing…