Search code examples
dictionaryinputempty-list

how would i make two empty lists and put them into a dictinary so that the names and numbers are associated and take user inputs


the objcetive it to have an empty list that gets inputs from a user and has a number associated with it. below is what ive tried:

name = []
number =[]
counter = 0
while counter != 3:
  name_grade = {
    name.append(str(input("enter your name: "))) : number.append(int(input("enter your number: ")))
    }
counter= counter + 1
print(name_number)

but i just get:

{None: None}

is there a better way of doing this and how would i do this better?


Solution

  • It doesn't work because there's no variable or dictionary in your code named name_number. The basic way to do this would be as follows:

    name = []
    number =[]
    counter = 0
    name_grade = {}
    
    while counter != 3:
        #taking the inputs:
        name_inp = str(input("Enter your name: "))
        number_inp = int(input("Enter your number: "))
        
        #adding to the dictionary
        name_grade[name_inp] = number_inp
        
        #adding to the lists
        name.append(name_inp)
        number.append(number_inp)
     
        counter= counter + 1
    print(name_grade)