Search code examples
pythondictionaryuser-input

How to store users input into dictionary? Python


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)

Solution

  • 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)