[閒聊] 女巫獵人random一直跑Trun?

作者: gogogo123 (不知所云)   2019-09-26 13:09:33
import random
class Witch:
def __init__(self,name,level):
self.name=name
self.level=level
def __repr__(self):
return ('{}(lv{})'.format(self.name,self.level))
def witchcraft_attack(self):
return random.randint(1,10)*self.level
class FireWitch(Witch):
def __init__(self,name,level,firestaff):
super().__init__(name,level)
self.firestaff=firestaff
def witchcraft_attack(self):
base_attack=super().witchcraft_attack()
if self.firestaff== True:
print("女巫噴火啦")
return base_attack*2
else:
return base_attack
firewitch=FireWitch('火焰女巫',5,random.choice([False,True]))
目前想說 random ture * 2  false * 1
但執行
firewitch.witchcraft_attack()
女巫噴火啦
Out[139]: 100
firewitch.witchcraft_attack()
女巫噴火啦
Out[140]: 80
firewitch.witchcraft_attack()
女巫噴火啦
Out[141]: 50
女巫一直噴火 感覺random 一直跑ture
為啥不會跑false呢?
作者: MARGHT   2019-09-26 13:46:00
因為你只有在初始時候決定跑或不跑
作者: TitanEric (泰坦)   2019-09-26 14:43:00
Boolean比較建議用is不要用==或者是直接if bool_val
作者: abr810416 (無敵鐵金剛)   2019-09-27 20:55:00
好奇為什麼用is比較好
作者: sxy67230 (charlesgg)   2019-09-28 09:27:00
Python 中的==是做值的比對,is是直接查詢位址,類似c的指針。雖然說效能上沒有明顯的差別,但是就是可讀性而已,一般你要做數值或是字串才建議用==

Links booklink

Contact Us: admin [ a t ] ucptt.com