|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
麻烦小伙伴看一下,在类中的方法调用类中初始化的变量属性,提示没有定义
- class Ticket:
- nor_Ticket = 100
- week_Ticket = nor_Ticket*(1.2)
- def calc_Ticket(self,adult,child,time):
- if time == 1:
- sum_Ticket = int(adult)*nor_Ticket + int(child)*(nor_Ticket/2)
- print("票价为【%s】"%sum_Ticket)
- return sum_Ticket
- elif time == 0:
- sum_Ticket = int(adult)*week_Ticket + int(child)*(week_Ticket/2)
- print("票价为【%s】"%sum_Ticket)
- return sum_Ticket
- else:
- return None
复制代码
这是我定义的一个票的类,在里面有一个属性nor_Ticket 初始化为100,在方法calc_Ticket里面调用了这个属性。在运行时出错,报如下错误:
- >>> q = Ticket()
- >>> q.calc_Ticket(2,1,1)
- Traceback (most recent call last):
- File "<pyshell#1>", line 1, in <module>
- q.calc_Ticket(2,1,1)
- File "E:\project\python\20170221.py", line 35, in calc_Ticket
- sum_Ticket = int(adult)*nor_Ticket + int(child)*(nor_Ticket/2)
- NameError: name 'nor_Ticket' is not defined
- >>>
复制代码
请问是我应用的问你还是怎么回事 |
|