我如何制作两个空列表并将它们放入字典中以便名称和数字相关联并接受用户输入
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
它的目标是有一个空列表,该列表从用户那里获取输入并有一个与之关联的数字。以下是我尝试过的:
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)
但我刚得到:
{None: None}
有没有更好的方法来做这件事,我该如何做得更好?
它不起作用,因为在您名为 name_number 的代码中没有变量或字典。
执行此操作的基本方法如下:
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)
它的目标是有一个空列表,该列表从用户那里获取输入并有一个与之关联的数字。以下是我尝试过的:
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)
但我刚得到:
{None: None}
有没有更好的方法来做这件事,我该如何做得更好?
它不起作用,因为在您名为 name_number 的代码中没有变量或字典。 执行此操作的基本方法如下:
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)