亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復此代碼中可被0整除的錯誤

如何修復此代碼中可被0整除的錯誤

海綿寶寶撒 2022-09-27 14:53:55
此代碼引發可被 0 整除的錯誤 for key in results_dic:    # match (if dog then breed match)    if results_dic[key][2] == 1:        # isa dog (pet label) & breed match        if results_dic[key][3] == 1:            n_pet_dog += 1            # isa dog (classifier label) & breed match            if results_dic[key][4] == 1:                n_class_cdog += 1                n_match_breed += 1        # NOT dog (pet_label)        else:            # NOT dog (classifier label)            if results_dic[key][4] == 0:                n_class_cnotd += 1    # NOT - match (not a breed match if a dog)    else:        # NOT - match        # isa dog (pet label)         if results_dic[key][3] == 1:            n_pet_dog += 1            # isa dog (classifier label)            if results_dic[key][4] == 1:                n_class_cdog += 1        # NOT dog (pet_label)        else:            # NOT dog (classifier label)            if results_dic[key][4] == 0:                n_class_cnotd += 1# calculates statistics based upon counters from aboven_pet_notd = n_images - n_pet_dogpct_corr_dog = ( n_class_cdog / n_pet_dog )*100pct_corr_notdog = ( n_class_cnotd / n_pet_notd )*100pct_corr_breed = ( n_match_breed / n_pet_dog )*100即使我使用if-else語句,它也會拋出相同的錯誤,我應該使用異常語句,我怎么能在這種情況下使用它,我已經被告知這是因為縮進錯誤,但我檢查了它,它應該沒有問題
查看完整描述

3 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

假設 和 是整數,我建議將分母更改為 和,以便您可以避免在那里使用 min 值,而是回退到最小值。示例:更改n_pet_dogn_pet_notdmax(n_pet_notd, 1)max(n_pet_dog, 1)01

pct_corr_dog = ( n_class_cdog / n_pet_dog )*100

pct_corr_dog = ( n_class_cdog / max(n_pet_dog,1) )*100


查看完整回答
反對 回復 2022-09-27
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

您可以使用三元運算符來獲取干凈的代碼。

pct_corr_dog = (n_class_cdog/n_pet_dog)*100 if (n_pet_dog != 0) else #what to do otherwise


查看完整回答
反對 回復 2022-09-27
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您可以通過不除以零來修復除以零。

n_pet_dog可能為零。這或多或少是不可避免的。可能沒有寵物狗。( n_class_cdog / n_pet_dog )

在這種情況下,試圖確定寵物狗的百分比是沒有意義的,所以你應該(a)不執行這個語句,并且(b)決定在沒有寵物狗的情況下,你想要你的代碼做什么。


查看完整回答
反對 回復 2022-09-27
  • 3 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號