+ 1
How can we do it
Your program needs to take the key as input and output the corresponding value. https://code.sololearn.com/c8mBqNtgml83/?ref=app
5 odpowiedzi
+ 6
Jovany William ,
instead of using:
inp = input()
if inp in car:
print(car[inp])
else:
print("not found")
you can use the dict .get() method like:
inp = input()
print(car.get(inp, "not found!"))
...
so you can omit checking if the key is existing. .get() allows a second argument, that will be output if key is not found.
+ 4
Jovany William
It's a dictionary. Take an input and using that input you can get value.
Use get function of dictionary or you can do like this:
print (car[input_key]) where input_key is user input.
+ 4
Allan 🔥STORMER🔥🔥🔥🔥 🅰🅹 🅐🅝🅐🅝🅣 thankss
+ 3
first of all declare/define your input as follows:
key = input("")
then use dictionary function of printing key output
print(car[key])
this will take your input as the dictionary key and output its value.
You can make your code more appealing by using if statement as below:
car = {
'brand':'BMW',
'year': 2018,
'color': 'red',
'mileage': 15000
}
key = input("")
if key in car:
print(car[key])
else:
print("not found!")
hope it helps.
+ 3
looks like you got your answer. but since i put all this together, here is my response.
"""
car is dictionary
brand, year, color, and mileage are the keys within the car dictionary.
each key has a value. the value for brand is BMW, year is 2018, color is red, and mileage is 15000.
you can access each value by placing the key within brackets of the dictionary.
car['brand']
BMW
car['year']
2018
car['color']
red
car['mileage']
15000
"""
# dictionary with key:value pair
car = {
'brand':'BMW',
'year': 2018,
'color': 'red',
'mileage': 15000
}
for key in car.keys(): # loop through dictionary keys
val=car[key] # use key to retrieve value from dictionary and store in variable named val
print(val) # print val
https://code.sololearn.com/cZO7r6lJK76s/?ref=app