![](/img/trans.png)
[英]TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType'
[英]python function handling giving error TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
我試圖讓我的代碼有點專業和基於我的代碼的功能如下:
def NOWT(nowt):#prints the number of words in the file
nowt=wdata['sentences'].str.split().map(len).sum()
print("Total number of the whole words: {}".format(nowt))
return
def NOW(now): #prints the number of Tokanized words in the file
now=data.str.split().map(len).sum()
print("Total number of tokanized words: {}".format(now))
return
def DIFF(diff):
diff=NOWT(wdata)-NOW(data)
print("The diff. is :{}".format(diff))
return
NOW(now)
NOWT(wdata)
DIFF(data)
當我嘗試獲取其他 2 個函數的輸出總和時,出現此錯誤:
diff=NOWT(wdata)-NOW(data)
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
任何想法我做錯了什么?
發生這種情況是因為所有函數都返回 None 而不是任何值。 我想您想要每個函數中第一個聲明的值。 正確的代碼是:
def NOWT(nowt):#prints the number of words in the file
nowt=wdata['sentences'].str.split().map(len).sum()
print("Total number of the whole words: {}".format(nowt))
return nowt
def NOW(now): #prints the number of Tokenized words in the file
now=data.str.split().map(len).sum()
print("Total number of tokanized words: {}".format(now))
return now
def DIFF(diff):
diff=NOWT(wdata)-NOW(data)
print("The diff. is :{}".format(diff))
return diff
NOW(data)
NOWT(wdata)
DIFF(data)
為防止打印兩次:
def NOWT(wdata):#prints the number of words in the file
nowt= wdata['sentences'].str.split().map(len).sum()
print("Total number of the whole words: {}".format(nowt))
return nowt
def NOW(data): #prints the number of Tokenized words in the file
now= data.str.split().map(len).sum()
print("Total number of tokenized words: {}".format(now))
return now
def DIFF(now, nowt):
diff=nowt-now
print("The diff. is : {}".format(diff))
return diff
now = NOW(data)
nowt = NOWT(wdata)
diff = DIFF(nowt, now)
另一種選擇是從回調中刪除倒數第二行和倒數第三行:
def NOWT(nowt):#prints the number of words in the file
nowt=wdata['sentences'].str.split().map(len).sum()
print("Total number of the whole words: {}".format(nowt))
return nowt
def NOW(now): #prints the number of Tokenized words in the file
now=data.str.split().map(len).sum()
print("Total number of tokanized words: {}".format(now))
return now
def DIFF(diff):
diff=NOWT(wdata)-NOW(data)
print("The diff. is :{}".format(diff))
return diff
DIFF(data)
你不能減去nonetype
。 您必須將函數 return 轉換為int
或float
才能使其工作。
試試看
diff=int(NOWT(wdata))-int(NOW(data))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.