Ziggy Witkowski
Ziggy Witkowski

Reputation: 81

To make Dictionary completely out of user Inputs

Is it possible to make a dictionary completely out of user Inputs? Example:

for x in range(1):
    name = input('Dict name :') # user enters: 'Car'
    k = input('Key: ')  # user enters: 'Toyota'
    v = input('Value')  # user enters: 'White'

# To have result: 
car= {'Toyota': 'White'}

Upvotes: 1

Views: 43

Answers (1)

Joaquín
Joaquín

Reputation: 350

from what i understand you want something like this right?

    array = []
    dict = {}
    for x in range(2):
        obj = {}
        dict_name = input('Name :')
        key = input('Key :')
        obj[key] = input('Value :')
        dict[dict_name] = obj
        print(dict)
    array.append(dict)
    
    # To have result: 
    # car= {'Toyota': 'White'}
    print(array)

basically what this does is we create an array first just to append all the objects we will be creating, in the loop we add the obj = {} so we can create a new object every time the loop begins and when the loop finished we append the dicts inside the array

this will result in:

 {'car': {'toyota': 'white'}, 'car': {'toyota': 'red'}}
[{'car': {'toyota': 'white'}, 'car': {'toyota': 'red'}}]

Upvotes: 1

Related Questions