简体   繁体   中英

Returning a formatted string in python

def compute_tax(gross_pay):
    tax = gross_pay * 0.15
    return "PHP {:,.2f}".format(tax)

I am trying to return the tax in a formatted string, but it generates an error TypeError: can't multiply sequence by non-int of type 'float' Why does this happen? The gross_pay is float. I also tested this function using pytest and it worked fine. But when I run the program itself, it crashes. What am I doing wrong?

If your other function is returning gross_pay as a string, you can always convert it to a float.

def compute_tax(gross_pay):
    gross_pay = float(gross_pay)
    tax = gross_pay * 0.15
    return "PHP {:,.2f}".format(tax)

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