我有一个观点,我正在尝试执行一个简单的数学计算,我收到的错误让我失望。
所以我有一个方法可以接受两个变量,一个十进制值和一个整数。我想取数字并将小数除以整数。我收到以下错误,我不知道为什么......
这是方法
def SplitEven(record, amount):
record_count = record.count
print(record_count)
print(amount)
split_amount = amount/record_count
print(split_amount)
rounded_amount = round(split_amount, 2)
print (record_count)
print (amount)
print (split_amount)
return rounded_amount
这是错误消息:
unsupported operand type(s) for /: 'str' and 'int'
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in addTransaction
taxSplit = SplitEven(record, amount)
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in SplitEven
split_amount = amount/record_count
以下是印刷陈述的内容:
[25/Jul/2017 16:14:10] "GET /static/css/blog.css HTTP/1.1" 404 1649
6
6
6.00
[25/Jul/2017 16:15:05] "POST /39/72/add_transaction/ HTTP/1.1" 500 83164
答案 0 :(得分:1)
from decimal import Decimal
def SplitEven(record, amount):
record_count = Decimal(record.count)
split_amount = Decimal(amount)/record_count
rounded_amount = round(split_amount, 2)
return rounded_amount