0

[Beginner Level] (Function,Dictionaries and Inputs)

def samfunc(z): x = [7,'8',9,1000] lty=[] for i in x: ty=type(i) lty.append(ty) dict1 = dict(zip(x,lty)) return print(z,'is',dict1.get(z,'not in the dictionary')) >>samfunc(9) 9 is <class 'int'> Guys I have created a simple function that creates dictionary from 2 lists. When running the code above, I am acquiring the desired output as per dictionary keys. The issue I am encountering is, when I'm calling the function using input method: y=input("Please enter anything:") samfunc(y) Even I input the keys given in the dictionary, the result is giving me "not in the dictionary" Can someone please explain to me why? Thanks

23rd Apr 2020, 8:32 AM
David Brent
David Brent - avatar
2 Answers
+ 2
Are you sure were inputting right keys?because input will return a string ,you need to convert it to int(input()) for integer input
23rd Apr 2020, 9:44 AM
Abhay
Abhay - avatar
+ 1
Thanks Abhya, this statement answers my question: "input will return a string" Thank you!!
23rd Apr 2020, 9:49 AM
David Brent
David Brent - avatar