请教一个Python的问题, 关于raw_input

Python010

请教一个Python的问题, 关于raw_input,第1张

你的代码我明白,你是想输完50,用一个逗号隔开紧跟着就打印一个字母L,是吧?这样在python中是行不通的,要这样写:

tank = int(raw_input("Size of tank: "))#输入并赋值

print "%sL" % tank#再将赋到的值连同L一起打印

试试看

Python 语句最后是不需要逗号的,程序修正如下:

size = int(raw_input('What is the size of your oil box:'))

percent = int(raw_input('What is the percent of your oil box:'))

speed = int(raw_input('How far can your go per L oil:'))

distance_cango = float(size * percent * speed) / 100

print 'Size of tank:',int(size)

print 'percent full:',int(percent)

print 'You can go another',distance_cango,'km'

print 'The next gas station is 200 km away'

if 200 - 5 <= distance_cango <= 200 + 5 :

print 'You can wait for the next station'

else :

print 'Get gas now!'

0down votefavorite

I'm a Python noob and am having some trouble with some inheritance learning. My code is throwing an attribute error.

class Battery():

"""A simple attempt to model a battery for an electric car."""

def __init__(self, battery_size=70):

"""Initialize the battery's attributes."""

self.battery_size = battery_size

def describe_battery(self):

"""Print a statement describing the battery size."""

print("\n" + "This car has a " + str(self.battery_size) +

'-kWh battery.')

def get_range(self):

"""Print a statement about the range based on the battery size."""

if self.battery_size == 70:

range = 240

elif self.battery_size == 85:

range = 270

message = self.make + " can go approximately " + str(range)

message += " miles on a full charge."

print(message)

class ElectricCar(Car):

"""Represents aspects of a car, specific to electric vehicles."""

def __init__(self, make, model, year):

"""

Initialize the attributes of the parent class.

Then initialize attributes specific to an electric car.

"""

super().__init__(make.title(), model, year)

self.battery = Battery()

def fill_gas_tank(self):

"""Electric cars don't have gas tanks."""

print(self.make + "'s " + "don't need a gas tank.")

my_tesla = ElectricCar('tesla', 'p90d', '2016')

print(my_tesla.get_descriptive_name())

my_tesla.battery.describe_battery()

my_tesla.battery.get_range()

I've played around with the coding and the attributes, but I can't seem to get it to run without an error. Any guidance is greatly appreciated. Traceback (most recent call last): File "C:\Users\n\Downloads\inheritance.py", line 184, in my_tesla.battery.get_range() File "C:\Users\n\Downloads\inheritance.py", line 158, in get_range message = self.make + " can go approximately " + str(range) AttributeError: 'Battery' object has no attribute 'make'