如果我执行AnimalsENUM(35).Name = 'TIGER'
,它将起作用,但如果我使用函数设置Name
则不行。为什么呢?
class AnimalsENUM():
def __init__(self,N):
try:
if N not in range(101):
raise ValueError
except ValueError:
print("Animal Number %s is not vaild" % (Number))
self.__Number = N
self.__Name = ''
self.__Age = None
self.__Location = None
设置名称的功能
def SetName(self):
while True:
try:
Name = str(input("input name in all caps"))
if Name not in ('TIGER', 'MONKEY','ELEPHANT', None):
raise ValueError
break
except ValueError:
print("Animal name %s not valid" % (Name))
self.__Name = Name
def SetLocation__(self):
while True:
try:
Location = int(input("input name in all caps"))
if Location not in range(21):
raise ValueError
except ValueError:
print("Animal Location %s not vaild" % (Location))
self.__Location = Location
def SetAge(self):
while True:
try:
Age = int(input("input name in all caps"))
if Age not in (range(100)):
raise ValueError
except ValueError:
print("Animal Age %s not vaild" % (Age))
我应该可以打印这个并用帽子打印动物名称
c = AnimalsENUM(25)
c.SetName()
print(c.Name)