我有4個變量,其中一些將是True和False,並且對於每個組合,我將不得不調用一個或幾個函數。 我目前對每個案例都使用了if else語句,我想知道是否有更好的方法來獲得與字典或其他內容相同的結果。如何簡化if語句中的布爾邏輯?
謝謝
這裏是我的代碼:簡化它會識別並分解出重複
if (self.cpe_ip and self.cpe_passwd) and self.phone and self.pppoe:
print "launch ISP portal, modem and radius"
if self.isp() == "east":
self.launchbell()
else:
self.launchtelus()
print 'check modem...'
self.modemstatus()
radius = sgp_radius.Radius(self.pppoe)
print 'check radius logs...'
self.data = radius.sgp()
self.radius_save()
#exit(0)
elif (self.cpe_ip and self.cpe_passwd) and not self.phone and not self.pppoe:
print "launch modem test only"
self.modemstatus()
#exit(0)
elif not(self.cpe_ip and self.cpe_passwd) and self.phone and not self.pppoe:
#print "only Bell portal"
if self.isp() == "east":
self.launchbell()
else:
self.launchtelus()
elif (self.cpe_ip and self.cpe_passwd) and not self.phone and self.pppoe:
#print "launch modem and radius test."
self.modemstatus()
radius = sgp_radius.Radius(self.pppoe)
print 'check radius logs...'
self.data = radius.sgp()
self.radius_save()
elif not(self.cpe_ip and self.cpe_passwd) and not self.phone and self.pppoe:
#print "only radius tests."
radius = sgp_radius.Radius(self.pppoe)
self.data = radius.sgp()
self.radius_save()
elif not(self.cpe_ip and self.cpe_passwd) and self.phone and self.pppoe:
print "bell and radius tests."
if self.isp() == "east":
self.launchbell()
else:
self.launchtelus()
radius = sgp_radius.Radius(self.pppoe)
print 'check radius logs...'
self.data = radius.sgp()
self.radius_save()
elif (self.cpe_ip and self.cpe_passwd) and self.phone and not self.pppoe:
#print "launch modem and bell tests."
if self.isp() == "east":
self.launchbell()
else:
self.launchtelus()
self.modemstatus()
else:
#print "test bell only"
#launchbell(self.phone)
exit(0)
您的快速回答OK THKS,這意味着我將不得不使用一個for循環來測試每個元素,並推出如果函數其中之一是真的... – neonet
@neonet我不太贊同你的評論。這三個獨立的'if'子句不需要循環,你可以把它們放在我給他們看的地方;一個接一個地在你的(我猜)測試函數中。 – jonrsharpe
如果他想根據不同的組合打印當前註釋掉的字符串,該怎麼辦? (或者更一般地說,如果明天邏輯改變,重複數量減少) – GreenAsJade