How to store users input into dictionary? Python

Viewed 13666

I want to create a simple app to Ask user to give name and marks of 10 different students and Store them in dictionary.

so far only key:value from the last input is stored into dictionary.

can you please check my code?

 marks = {}

    for i in range(10):
        student_name = input("Enter student's name: ")
        student_mark = input("Enter student's mark: ")


marks = {student_name.title():student_mark}

print(marks)
2 Answers

Your current code has two issues: first, it doesn't save the values of each student inside the loop. Second, it always rewrites the entire dictionary with a single key/value pair, that's why it doesn't work.

marks = {}

for i in range(10):
    student_name = input("Enter student's name: ")
    student_mark = input("Enter student's mark: ")
    marks[student_name.title()] = student_mark

print(marks)

You need your code to be inside your loop. Also the way you put value inside your dict is not right. This should do the trick

marks = {}

for i in range(10):
    student_name = input("Enter student's name: ")
    student_mark = input("Enter student's mark: ")

    marks[student_name] = student_mark

print(marks)
Related