Python-typeerror:not all arguments converted during string formatting

Source: Internet
Author: User

Where?

Run Python program, error appears in this line return "Unknow Object of%s"% value

Why?

%s means the value variable is loaded as a string, but the value is a Python tuple, and the tuple in Python cannot be formatted directly with%s and%, the error

The same?

Format a string using format or Format_map 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"% valueif __name__ = = ' __main__ ':    print (Use_type)    # passed tuple parameter    Print (Use_type ((1, 3))

Correcting code

def use_type (value):    if Type (value) = = Int:        return "int"    elif type (value) = = float:        return "float"    else:        # format        return "Unknow Object of {value}". Format (Value=value)        # Format_map Way        # return "Unknow Object of {value} ". Format_map ({        #     " value ": Value        #}) If __name__ = = ' __main__ ':    print (Use_type (10)    # Pass-through tuple parameter    print (Use_type ((1, 3)))

  

Python-typeerror:not all arguments converted during string formatting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.