Dictionaries | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2


#creating a dictionary that has student data which has their age with name. user enters a name to get the age, first user entry searches the dictionary and then validate if its there or not. I am missing the serach step here that will be for loop. it doesn't work when I include it. please help. student_data = {'Sam': 23, 'Dave': 25,'Adam': 28} input = input("student name") n = input if n in student_data: print(student_data[n]) else: print("not in dictionary ")

27th Apr 2022, 1:27 PM
Knowledge Is Power
Knowledge Is Power - avatar
3 Answers
+ 5
Hello Knowledge Is Power Your code works fine. I'm not sure if I understand you but if n in student_data: -> this is your searching step. There is no need to use a loop.
27th Apr 2022, 1:42 PM
Denise Roßberg
Denise Roßberg - avatar
+ 5
Knowledge Is Power If you don't want if else statement then you can also use get function of dictionary student_data = {'AJ' : 32, 'Anant' : 30} n = input() print (student_data.get(n, 'not found'))
27th Apr 2022, 3:01 PM
A͢J - avatar
+ 3
I thought i was a missing a step to search.Thank you for your support👍
27th Apr 2022, 2:54 PM
Knowledge Is Power
Knowledge Is Power - avatar