鱼C论坛

 找回密码
 立即注册
查看: 1236|回复: 2

[已解决]python 037 票价计算问题

[复制链接]
发表于 2018-2-22 23:04:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码无法运行,但不知道错在哪里,希望大神指教

代码运行显示错误如下:
>>> a = T(adult =2,child = 0)
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    a = T(adult =2,child = 0)
  File "C:/Users/lxx/Desktop/python练习题/票价计算.py", line 13, in __init__
    self.adultprice = self.price * self.adult
AttributeError: 'T' object has no attribute 'adult'

代码如下:
class T():
    def __init__(self,day = "day",adult = 0,child = 0):
   
        self.dayprice = 100
        self.wenkeenprice = self.dayprice * 1.2
        self.childprice = self.dayprice / 2

        if day == "day":
            self.price = self.dayprice
        else:
            self.price = self.wenkeenprice

        self.adultprice = self.price * self.adult
        self.childprrice = (self.price * self.child) / 2

    def countprice(self):
        self.num = self.manprice + self.childprice
        print(self.num)
最佳答案
2018-2-22 23:26:06
  1. def __init__(self, day="day", adult=0, child=0):
  2.     self.dayprice = 100
  3.     self.wenkeenprice = self.dayprice * 1.2
  4.     self.childprice = self.dayprice / 2
  5.     self.day = day
  6.     self.adult = adult
  7.     self.child = child

  8.     if day == "day":
  9.         self.price = self.dayprice
  10.     else:
  11.         self.price = self.wenkeenprice

  12.     self.adultprice = self.price * self.adult
  13.     self.childprrice = (self.price * self.child) / 2
复制代码

__init__()里要加几句。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-22 23:26:06 | 显示全部楼层    本楼为最佳答案   
  1. def __init__(self, day="day", adult=0, child=0):
  2.     self.dayprice = 100
  3.     self.wenkeenprice = self.dayprice * 1.2
  4.     self.childprice = self.dayprice / 2
  5.     self.day = day
  6.     self.adult = adult
  7.     self.child = child

  8.     if day == "day":
  9.         self.price = self.dayprice
  10.     else:
  11.         self.price = self.wenkeenprice

  12.     self.adultprice = self.price * self.adult
  13.     self.childprrice = (self.price * self.child) / 2
复制代码

__init__()里要加几句。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-22 23:33:21 | 显示全部楼层
°蓝鲤歌蓝 发表于 2018-2-22 23:26
__init__()里要加几句。

是的,刚刚发现了需要重新赋值才可以在函数里调用。感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表