+ 2
Dictionaries
#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 ")
3 Respuestas
+ 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.
+ 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'))
+ 3
I thought i was a missing a step to search.Thank you for your support👍