Axroxx
Axroxx

Reputation: 53

Get value from dictionary list value

I made a code where you create custom vehicles with license plates and info about them and I use dictionaries to keep track of them, and lists to keep track of what each car contains. So the key in the dictionary becomes the license plate and the list of attributes of the car becomes the value. Now I need to get each value separately in a print from the list.

I tried calling for values in the list as seen below with [] after the value but it didn't seem to work. The While loop is now working, apart from before.

carList = {"ABC 123": ["Mustang GT", 1969, "Black"]}
car = str(input("What car would you like to withdraw? Write the license plate")).upper()

car = str(input("What car would you like to withdraw? Write the license plate: ")).upper()

while (car in carList.keys()) == 0 and car != "0":
  car = str(input("That car doesen't exist. Write a new one or press 0: ")).upper()

choice = str(input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}".format(car, carList[car[0]], carList[car[1]], carList[car[2]])))

I just got an invalid syntax, and I want to get the value of each car printed.

Upvotes: 1

Views: 239

Answers (2)

Axroxx
Axroxx

Reputation: 53

This is the final code I ended up with:

carList = {"ABC 123": ["Mustang GT", 1969, "Black"]}
while True:
  car = str(input("What car would you like to withdraw? Write the license plate: ")).upper()
  while (car in carList.keys()) == 0 and car != "0":
    car = str(input("That car doesen't exist. Write a new one or press 0: ")).upper()
  if car == "0":
    break
  choice = str(input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}".format(car, carList[car][0], carList[car][1], carList[car][2])))
  a = input("Press enter to continue: ")
       break

The print:

What car would you like to withdraw? Write the license plate: abc 123
You wish to withdraw ABC 123
Model: Mustang GT
Year: 1969
Colour: Black

Upvotes: 1

John Szakmeister
John Szakmeister

Reputation: 46992

You've got quite a bit going on here that's not quite right. Personally, I'd organize the problem a little differently.

First, you don't need to call str() in the result of input()--it's already a string.

Second, you're call to .upper in the while loop is missing parens--it should be .upper(). Also the conditional for the while loop is not correct. car in carList does return a boolean (True or False) and Python will allow comparison of them to 1 and 0, so that parts okay but not really the idiomatic way of writing it. You'd typically say car not in carList and drop the == 0 portion. Also, car != 0 will always be true because if the user types 0 at the prompt, you actually get back the string '0' which is not equal to the integer 0.

Finally, the way you're trying to pull out the data for a specific car in carList is wrong. Here's the snippet:

carList[car[0], carList[car[1], carList[car[2]]

I can't really tell what the intention was here, but it's definitely a syntax problem. You're missing a closing ] in there at the very least, and depending on what you meant, you may not have enough arguments. It looks like you probably meant to write:

carList[car[0]], carList[car[1]], carList[car[2]]

In this case, you're trying to look up the vehicle by one character of the license plate. Substituting, you get:

carList['A'], carList['B'], carList['C']

And it's pretty clear it's not what you want. Instead, you want to grab the list for car. You get that by using the whole value of car:

carList[car]

That gets you the whole list. Now you want the individual elements, so you'd write:

carList[car][0], carList[car][1], carList[car][2]

Much better is to simply grab the list and stash it in a variable, and then use the new variable to get the data elements:

data = carList[car]
data[0], data[1], data[2]

In the end, I'd likely write something closer to this:

carList = {"ABC 123": ["Mustang GT", 1969, "Black"]}

while True:
    car = input("What car would you like to withdraw? Write the license plate: ").upper()

    if car == '0':
        break

    if car not in carList:
        print("That car doesn't exist. Press 0 to abort or write a new license plate.")
        continue

    data = carList[car]

    choice = input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}\n: ".format(
        car, data[0], data[1], data[2]))

    # ...

update: Based on your comment below, this could be helpful:

class Car:
    def __init__(self, model, year, color):
        self.model = model
        self.year = year
        self.color = color

carList = {"ABC 123": Car("Mustang GT", 1969, "Black")}

# ...

    choice = input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}\n: ".format(
    car, data.model, data.year, data.color))

Upvotes: 1

Related Questions