作者gogogo123 (狗狗睾丸被割)
看板Python
标题[闲聊] 女巫猎人random一直跑Trun?
时间Thu Sep 26 13:09:33 2019
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呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 117.56.227.246 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1569474575.A.19B.html
※ 编辑: gogogo123 (117.56.227.246 台湾), 09/26/2019 13:10:23
※ 编辑: gogogo123 (117.56.227.246 台湾), 09/26/2019 13:29:32
1F:→ MARGHT: 因为你只有在初始时候决定跑或不跑 09/26 13:46
2F:→ TitanEric: Boolean比较建议用is不要用== 09/26 14:43
3F:→ TitanEric: 或者是直接if bool_val 09/26 14:44
4F:推 abr810416: 好奇为什麽用is比较好 09/27 20:55
5F:推 sxy67230: Python 中的==是做值的比对,is是直接查询位址,类似c 09/28 09:27
6F:→ sxy67230: 的指针。虽然说效能上没有明显的差别,但是就是可读性 09/28 09:27
7F:→ sxy67230: 而已,一般你要做数值或是字串才建议用== 09/28 09:27