0

Help!

This project called "car data". You are working at a car dealership and store the car data in a dictionary: Your program needs to take the key as input and output the corresponding value. Sample Input: year Sample Output: 2018 The data is already defined in the code. I wrote this code: car = { 'brand':'BMW', 'year': 2018, 'color': 'red', 'mileage': 15000 } x = input() for x in car: if x == 'brand': print (car['brand']) elif x == 'year': print (car['year']) elif x == 'color': print (car['color']) elif x == 'mileage': print (car['mileage']) else: print ('Nothing!') but it turns out to be : input : brand output : BMW 2018 red 15000 Can you guys help me to edit the code above to let it meets the criteria? Thanks.

17th May 2021, 6:36 AM
SOON YEE FENG Moe
4 Answers
+ 1
for input like "brand year color" for a in.split(): print(car[a])
17th May 2021, 6:53 AM
minirkk
minirkk - avatar
+ 5
SOON YEE FENG Moe It is a dictionary, you can access value by key directly without using loop or if else condition So Just do this print(car[x]) Nothing else
17th May 2021, 6:43 AM
A͢J
A͢J - avatar
+ 2
thanks
17th May 2021, 6:49 AM
SOON YEE FENG Moe
0
follow you already
17th May 2021, 6:50 AM
SOON YEE FENG Moe