220k views
2 votes
Write a program that defines the following two lists:

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]
These lists match up, so Alice’s age is 20, Bob’s age is 21, and so on. Write a program
that asks the user to input the number of the person to retrieve the corresponding
data from the lists. For example, if the user inputs 1, this means the first person
whose data is stored in index 0 of these lists. Then, your program should combine
the chosen person’s data from these two lists into a dictionary. Then, print the
created dictionary.
Hint: Recall that the function input can retrieve a keyboard input from a user. The
signature of this function is as follows:
userInputValue = input("Your message to the user")
N.B.: userInputValue is of type String

User Dktaylor
by
8.5k points

1 Answer

5 votes

Answer: I used colab, or use your favorite ide

def names_ages_dict():

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]

# merging both lists

names_ages = [list(x) for x in zip(names, ages)]

index = []

# creating index

i = 0

while i < len(names):

index.append(i)

i += 1

# print("Resultant index is : " ,index)

my_dict = dict(zip(index, names_ages))

print('Input the index value:' )

userInputValue = int(input())

print(f'data at index {userInputValue} is, '+ 'Name: ' + str(my_dict[input1][0] + ' Age: ' + str(my_dict[input1][1])))

keys = []

values = []

keys.append(my_dict[input1][0])

values.append(my_dict[input1][1])

created_dict = dict(zip(keys, values))

print('The created dictionary is ' + str(created_dict))

names_ages_dict()

Explanation: create the function and call the function later

Write a program that defines the following two lists: names = ['Alice', 'Bob', 'Cathy-example-1
User Mujahid Khan
by
7.7k points