【发布时间】:2021-12-31 18:12:15
【问题描述】:
所以我一直在制作一个简单的程序,您可以在其中选择汽车并购买/出售它们,并将它们存储在库存中。此外,您可以询问您想要的汽车的特定属性。代码如下:
class Consumer:
#create an inventory to store cars
garage = []
def __init__(self, budget):
self.budget = budget
#returns current budget of user
def checkBudget(self):
return self.budget
#take money away from the budget and store the car in the inventory
def buy(self, car):
self.budget = self.budget - car.price
self.garage.append(car)
def sell(self, value):
#takes back original money and takes out car
self.budget = self.budget + self.garage[value].getPrice()
return self.garage.pop(value)
def showGarage(self):
for i in self.garage:
print(i.getInfo())
def carCount(self):
return len(self.garage) - 1
class Car:
def __init__(self, model, color, price, year):
self.model = model
self.color = color
self.price = price
self.year = year
def getName(self):
return ("{} {} from {}".format(self.color, self.model, self.year))
def getInfo(self):
return ("{} {} from {} that costs ${}".format(self.color, self.model, self.year, self.price))
def getColor(self):
return self.color
def getYear(self):
return self.year
def getPrice(self):
return self.price
class Sedan(Car):
def __init__(self, model, color, price, year):
super().__init__(model, color, price, year)
def colorPrice(self, color):
self.color = color
self.price = self.price + 200
class SUV(Car):
def __init__(self, model, color, price, year):
super().__init__(model, color, price, year)
def colorPrice(self, color):
self.color = color
self.price = self.price + 300
class Sports(Car):
def __init__(self, model, color, price, year):
super().__init__(model, color, price, year)
def colorPrice(self, color):
self.color = color
self.price = self.price + 800
class Bike(Car):
def __init__(self, model, color, price, year):
super().__init__(model, color, price, year)
def colorPrice(self, color):
self.color = color
self.price = self.price + 400
class Catalogue:
#placeholder to store all cars
whole = []
#sets user-defined preferences of cars
preferences = []
def __init__(self):
pass
def addCars(self, car):
self.whole.append(car)
self.preferences.append(car)
#shortens list so that the catalogue shows only the cars from the year the user wants from
def year(self, year):
for i in self.preferences:
if int(i.getYear()) != int(year):
self.preferences.remove(i)
if not self.preferences:
print("There are no cars from this year")
self.Reset()
print(self.preferences)
#shortens list so that the catalogue shows only the cars of a certain color
def color(self, color):
for i in self.preferences:
if str(i.getColor()) != str(color):
self.preferences.remove(i)
if not self.preferences:
print("There are no cars of this color")
self.Reset()
print(self.preferences)
#shortens list so that it only shows the price range
def price(self, low, high):
for i in self.preferences:
if int(i.getPrice()) <= int(low) or int(i.getPrice()) >= int(high):
self.preferences.remove(i)
if not self.preferences:
print("There are no cars from this price range")
self.reset()
#clears all preferences so that it shows all available cars
def Reset(self):
self.preferences.clear()
for i in self.whole:
self.preferences.append(i);
def showCatalogue(self):
for i in range(len(self.preferences)):
print(self.preferences[i].getInfo())
def getLength(self):
return len(self.preferences)
#gives the car away
def sell(self, value):
self.whole.pop(value)
a = self.preferences.pop(value)
return a
showcase = Catalogue()
Toyota = Sedan('Toyota Camry', 'blue', 64000, 2002)
Honda = Sedan('Honda Civic', 'red', 65000, 2002)
Dodge = SUV('Dodge Durango', 'black', 4324, 2015)
Yamaha = Bike('Yamaha x', 'red', 23240, 2015)
showcase.addCars(Toyota)
showcase.addCars(Honda)
showcase.addCars(Dodge)
showcase.addCars(Yamaha)
customer = Consumer(100000)
#garage module
def garage():
global customer
global showcase
a = input("What do you want to do? 1) check car collection 2) sell car 3) back ")
#shows all cars from the inventory
if int(a) == 1:
customer.showGarage()
garage()
#sells available cars
elif int(a) == 2:
customer.showGarage()
#will cancel if there care no cars in the inventory
if len(customer.garage) == 0:
print("You have no cars")
garage()
else:
#enter index no to select a car
b = input("Pick a car from 0 to {} ".format(customer.carCount()))
if int(b) > int(customer.carCount()):
print("You don't have that many cars!")
garage()
else:
c = input("Are you sure you want to sell this car? 1) yes 2) no ")
#asks for warning confirmation
if int(c) == 1:
d = customer.sell(int(b))
showcase.addCars(d)
print("This car has been sold; have a nice day!")
garage()
elif int(c) == 2:
garage()
#go back to main
elif int(a) == 3:
e = input("Are you sure you wanna go back? 1) yes 2) no ")
if int(e) == 1:
main()
elif int(e) == 2:
garage()
#asks if you want to specify the list
def access():
global showcase
showcase.showCatalogue()
print()
g = input("Looking for specifics? 1) yes 2) no: ")
if int(g) == 1:
options()
else:
choose()
print()
#would you like to access catalogue or garage?
def main():
a = input("Where do you want to go next? 1) check garage 2) browse more cars" )
if int(a) == 1:
garage()
elif int(a) == 2:
access()
else:
print("Try again")
main()
#car purchase confirmation
def buy(car):
global customer
global showcase
a = input("Are you sure you want to buy this car? 1) yes 2) no " )
if int(a) == 1:
#cancels purchase automatically if your balance is low
if int(customer.checkBudget()) < int(showcase.preferences[car].getPrice()):
print("You do not have enough money")
access()
else:
#takes the car out of the catalogue and stores it in the garage
print("Transaction taking place...")
sold = showcase.sell(car)
customer.buy(sold)
print("You purchased this car")
print("You now have a budget of {} dollars".format(customer.checkBudget()))
main()
def choose():
#which car you would like to purchase
global showcase
c = input("Which car would you like? enter from 0 to {} ".format(showcase.getLength()-1))
if int(c) > int(showcase.getLength()):
print("Out of bounds, please try again")
choose()
else:
buy(int(c))
def options():
#which specifics are you looking for?
global showcase
a = input("What are you looking for? 1) color 2) price 3) year: 4) reset ")
if int(a) == 1:
b = input("Pick a color: ")
showcase.color(b)
elif int(a) == 2:
b = input("Pick a lower price range: ")
c = input("Pick an upper price range: ")
if showcase.price(b) > showcase.price(c):
print("Invalid Error")
else:
showcase.price(b,c)
elif int(a) == 3:
b = input("Pick a year: ")
showcase.year(b)
elif int(a) == 4:
showcase.Reset()
else:
print("Invalid, try again")
options();
access()
access()
我的主要问题是细节。例如,当我输入一辆蓝色汽车时,它给了我一辆黑色汽车。当我输入一辆 2015 年的汽车时,它给了我一辆 2002 年的汽车。当我要一辆不超过 50000 美元的汽车时,它给了我一辆价值 65000 美元的汽车。以下是一些运行时场景:
What are you looking for? 1) color 2) price 3) year: 4) reset #1
Pick a color: #blue
#returns
blue Toyota Camry from 2002 that costs $64000
black Dodge Durango from 2015 that costs $4324
What are you looking for? 1) color 2) price 3) year: 4) reset #3
Pick a year: #2015
#returns
red Honda Civic from 2002 that costs $65000
black Dodge Durango from 2015 that costs $4324
red Yamaha x from 2015 that costs $23240
代码问题可以在这里找到:
#shortens list so that the catalogue shows only the cars of a certain color
def color(self, color):
for i in self.preferences:
if str(i.getColor()) != str(color):
self.preferences.remove(i)
if not self.preferences:
print("There are no cars of this color")
self.Reset()
print(self.preferences)
#shortens list so that it only shows the price range
def price(self, low, high):
for i in self.preferences:
if int(i.getPrice()) <= int(low) or int(i.getPrice()) >= int(high):
self.preferences.remove(i)
if not self.preferences:
print("There are no cars from this price range")
self.reset()
【问题讨论】:
标签: python arrays list class attributes