不確定它爲什麼超出範圍。餘設置的範圍從0到5 這是我的代碼獲取列表分配索引超出範圍
class Car(object):
def __init__ (self, price, speed, fuel, mileage):
self.price = price
self.speed = speed
self.fuel = fuel
self.mileage = mileage
self.price = price
if price > 1000:
self.tax = 15
else:
self.tax = 12
def displayAll(self):
print "Price: " + str(self.price)
print "Speed: " + str(self.speed)
print "Fuel: " + str(self.fuel)
print "Mileage: " + str(self.mileage)
print "Tax: 0." + str(self.mileage)
auto = [5]
for car in range(0,5):
price = input("How much does the car cost? ")
speed = input("Mile per hour? ")
mileage = input("Mile per gallon? ")
fuel = raw_input("How much fuel? ")
print car
auto[car] = Car(price, speed, fuel, mileage)
從一個空列表開始,並使用'append()'。 –