I need some help with a code I'm working on. The code starts off by creating a class for types of cars. Once that's done, I have it construct a list of predetermined vehicles (all made from the aforementioned class), and then I ask for user input, to see if the type of car they own is in the database.

When I input a type of car that the database doesn't recongize (say, a Prius), it asks to create a new item in the list so it contains the information for - theoretical - future inputs. However, the "if x in list" feature doesn't appear to work, because even if I enter the name of a product that is inside the list (a Viper) it doesn't recognize it.

Saying that, I have two questions: How do I get the list to print the names, manufacturer, and price of the car, instead of the standard "<__main__.carType instance at 0x6d400>" stuff? And why does the program not recognize objects that are in or have been appended to the list? Any advice or assistance would be greatly appreciated.

Here's the code.

class carType:
   def __init__(self, name, brand, price):
      self.name = name
      self.brand = brand
      self.price = price
      
carlist = [
   carType("Viper", "Dodge", 80000),
   carType("Mustang", "Ford", 25000),
   carType("Silhouette", "Oldsmobile", 26000),
   carType("Croma", "Fiat", 15000),
   carType("RL", "Acura", 46000),
   carType("Axiom", "Isuzu", 24000)
   ]

while True:
   x = raw_input("Enter the name of your car: ")
   if x in carlist:
      print "The car you specified is in our database."
      print "Your car was built by", carType.brand, ". and the general MSRP is", carType.price, "."
      break
   else:
       print "Your car is not in the database."
       print "Please enter the manufacturer and suggested retail price (MSRP)."
       y = raw_input("Who is the manufacturer of your vehicle? ")
       z = input("What is the suggested retail price? ")
       a = carType(x, y, z)
       carlist.append(a)
       print carlist
       break

1. You control the way your Car class prints by adding an __str__ method to it. Here is an example:

def __str__(self):
    s = self.brand, self.name +": $", self.price #eg: Dodge Viper: $80000
    return s

2. Because you're not checking the list properly. You are asking if the string the user has entered is in the list (it's not), not if the the list has a car with a name that matches the users entry. Here is a function that can help:

def car_in_list(l, n):
    for car in l:
        if n == car.name:
            return True #car with matching name found
    else:
        return False #no car found with name

Instead of if x in carlist , you would use if car_in_list(carlist, x)

Your carlist is a list of instances of the class carType. To compare the car's name you have to use the instance.name as shown here:

class carType:
   def __init__(self, name, brand, price):
      self.name = name
      self.brand = brand
      self.price = price
      
carlist = [
   carType("Viper", "Dodge", 80000),
   carType("Mustang", "Ford", 25000),
   carType("Silhouette", "Oldsmobile", 26000),
   carType("Croma", "Fiat", 15000),
   carType("RL", "Acura", 46000),
   carType("Axiom", "Isuzu", 24000)
   ]

print(carlist)  # shows a list of class instances

car_name = "Mustang"
for instance in carlist:
    print(instance.name)  # test
    if car_name == instance.name:
        print("%s is in the list" % car_name)

As you study some more about Python classes, it all will make sense to you.

Thank you both! I figured it out with the help you gave me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.