所以我正在学习Python,所以请原谅我的天真。
我在类上做了一些练习,这样当用户输入他们的车时,它会寻找实例化的对象,然后返回价格。
我让它工作的“宝马”-但当我尝试法拉利-它只返回宝马(20k)的价格。我认为这是一些不正确的价格检查功能部分的代码。
class Car:
car_list = []
def __init__(self, make, model, price):
self.make = make
self.model = model
self.price = price
self.car_list.append(self)
#this is the part of the code that i'm stuck with
def price_check(self):
for i in Car.car_list:
if New_Car.make == self.make:
return i.price
else:
print("Not available")
BMW = Car('BMW', '1 Series', "£20,000")
Ferrari = Car('Ferrari', 'Italia', "£90,000")
New_Car = Car(
input("What is make of your car? "), input("What is the model? "), "")
print("The cost of this car is: ", New_Car.price_check())
所以本质上,我希望它返回法拉利的价格,如果这是用户键入的。
最后
以上就是醉熏小土豆最近收集整理的关于python3获取用户输入_基于用户输入从Python中的类对象获取值的全部内容,更多相关python3获取用户输入_基于用户输入从Python中内容请搜索靠谱客的其他文章。
发表评论 取消回复