python小白,请教大家一个问题!

weixin_37577844 2017-12-06 11:20:16
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer.")
def increment_odometer(self, miles):
self.odometer_reading += miles
def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' '+ self.model
return long_name.title()
def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")

class Battery():
def __init__(self,battery_size = 70):
self.battery_size = battery_size
def describe_battery(self):
print("This car has a "+ str(self.battery_size) + "-KWH battery.")
def get_range(self):
if self.battery_size == 70:
range = 240
elif self.battery_size == 85:
range = 70
message = "This car can go approximately "+str(range)
message += "miles on a full charge."
print(message)
def upgrade_battery(self):
if self.battery_size != 85:
self.battery_size = 85
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery = Battery()
def fill_gas_tank():
print("This cr doesn't need a gas tank!")


以上时我创建的一个模块,保存为car.py,用ipython note book编辑。
我想在另外一个新建文件中导入这个模块中的Car函数,可是出错,如下:
...全文
169 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_37577844 2017-12-06
  • 打赏
  • 举报
回复
引用 1 楼 oTHUNDER 的回复:
没有null这个关键字啊,为空的话直接用引号中间没内容就行了 ''表示空字符串
不好意思,没听明白,还麻烦您指出在哪里如何修改,引号中间是指哪儿
weixin_37577844 2017-12-06
  • 打赏
  • 举报
回复
不好意思,没太懂,能麻烦具体指出需要修改哪里吗
THUNDER 2017-12-06
  • 打赏
  • 举报
回复
没有null这个关键字啊,为空的话直接用引号中间没内容就行了 ''表示空字符串
Hanson_Jun 2017-12-06
  • 打赏
  • 举报
回复
null是一个变量,这个变量你没有定义,所以报错
extend 2017-12-06
  • 打赏
  • 举报
回复
试过了,并不报错,可引用,没问题,IDLE+Cpython. 换个解释器试试,很可能解释器相关。
碧水幽幽泉 2017-12-06
  • 打赏
  • 举报
回复
把null换成'',即:两个单引号

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧