19th Mar 2022, 4:30 AM
Rohit Saha
Rohit Saha - avatar
5 Réponses
+ 4
car = { 'brand':'BMW', 'year': 2018, 'color': 'red', 'mileage': 15000 } x=input() if x == 'brand': print("BMW") elif x == 'year': print(2018) elif x == 'color': print('red') else: print(15000)
19th Mar 2022, 4:31 AM
NEZ
NEZ - avatar
+ 2
Rohit Saha No need of if else As car is a dictionary so you can use get function print (car.get(input()))
19th Mar 2022, 4:42 AM
A͢J
A͢J - avatar
+ 1
Thanks a lot NEZ for answering my question thanks 😊😊😊😊
19th Mar 2022, 4:35 AM
Rohit Saha
Rohit Saha - avatar
+ 1
NEZ , why? You have already defined a dictionary! x=input() print(car[x]) #thats it. No need to if elif else block.
19th Mar 2022, 5:25 AM
Shadoff
Shadoff - avatar
0
Shadoff I just corrected his code rather than using other ways. I was going through his logic. // You can use AJ's code too.
19th Mar 2022, 7:58 AM
NEZ
NEZ - avatar