-2

I want a feature where i can input new values into a dictionary to store and update so i can use it as part of code. Thanks

name_of_students = {'Sam':92,  'james':78, 'ray':67, 'jane':60, 'julie':89, 'mel':50} 
#add new variable into dict
new_students = {}
new_students = input("Enter the name of the student/score: ")
name_of_students = name_of_students.update(new_students)
print(name_of_students)

input mark:90

error: raceback (most recent call last): File "/Users/sonter/Documents/CODE X/grading-program-/.py", line 10, in <module> name_of_students = name_of_students.update(new_students) ValueError: dictionary update sequence element #0 has length 1; 2 is required

2 Answers2

0

here:

name_of_students = {'Sam':92,  'james':78, 'ray':67, 'jane':60, 'julie':89, 'mel':50} 
print(name_of_students)
#add new variable into dict
#name_of_students = {}
new_students = input("Enter the name of the student/score: ")
new_students = {new_students.split(':')[0] : int(new_students.split(':')[1])}
name_of_students = name_of_students.update(new_students) or name_of_students
print(name_of_students)

output:

{'Sam': 92, 'james': 78, 'ray': 67, 'jane': 60, 'julie': 89, 'mel': 50}
Enter the name of the student/score: mark:90
{'Sam': 92, 'james': 78, 'ray': 67, 'jane': 60, 'julie': 89, 'mel': 50, 'mark': 90}

from : Why doesn't a python dict.update() return the object?

pippo1980
  • 2,181
  • 3
  • 14
  • 30
0
#insert names of students



name_of_students = {'Sam':92,  'james':78, 'ray':67, 'jane':60, 'julie':89, 'mel':50} 
#add new variable into dict
new_students = {}
new_students = input("Enter the name of the student/score: ")
name_of_students = {new_students.split(
    ":")[0]: int(new_students.split(":")[1])}

print(name_of_students)

Use index to split input and int function to declear integer as input on the [1] index