My purpose is to get two lists into dictionary format and print out the Names whose age is 20. Here is my code. The problem that I am facing is that, after I obtain input from user and it pass to function it does not take effect. I can print out the parameter integer and age separately - it works fine. When it gets to the if function it does not print the name at all. I tried to hard code the integer to 20 - it works but that is not code that I want.
NAMES = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank','Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']
AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
list1 = {}
def getinput():
age=input('Please enter your age')
return age
def changedict():
for x in range(len(NAMES)):
list1[NAMES[x]]=AGES[x]
def get_names(integer):
for name, age in list1.items():
if age == integer:
print(name)
changedict()
para=getinput()
get_names(para)
If there is any improvement in the code above, like a more simplified version, please comment on it. I also want to know a bit more about the items
function. Is there any other way to scan through dictionary without using items
?