I have been trying for some time now to take user input and add it to an existing dictionary. Then print the dictionary keys and values with a for loop but I can't figure out the proper way to do this.
After research I am almost certain I am not adding the user inputted values to the dict correctly and when the code is run i get an error stating that 'set' is subscriptable. How should I add the user data to the dict and also print it properly?
# Authorized Users
authorized_users = ["Jim","Mary","Claire","Hector","Ren"]
# Student Dictionary
students ={
"Alice":{"id": "1", "age":26, "grade":"A"},
"Bob":{"id": "2", "age":34, "grade":"C"},
"Jimbo":{"id": "3", "age":12, "grade":"B"},
"Karen":{"id": "4", "age":33, "grade":"D"},
"Keith":{"id": "5", "age":53, "grade":"F"}
}
while True:
login = input("Please login with an authorized user: ").strip().title()
if login in authorized_users:
print("\nWelcome back "+login+", please choose an option from the menu!")
print(
'''
1.) View Student List
2.) View Authorized Users
3.) Add student
4.) Remove Student
5.) Exit program
'''
)
authorized_input = int(input("Select an option from the list above to proceed.: "))
if authorized_input == 1:
print("\n*** ACTIVE STUDENTS ***")
for student_id, student_info in students.items():
print("\nName:", student_id)
for key in student_info:
print(key + ':', student_info[key])
print("\n")
elif authorized_input == 2:
print("\n*** AUTHORIZED USERS ***")
for x in range(len(authorized_users)):
print(authorized_users[x])
print("\n")
elif authorized_input == 3:
print("Please fill in the new students information")
student_name = input("Enter the student's name: ")
student_id = input("Enter the student's ID: ")
student_age = input("Enter the student's age: ")
student_grade = input("Enter the student's grade: ")
# Add data to dict students
students[student_name] = {student_id, student_age, student_grade}
for student_id, student_info in students.items():
print("\nName:", student_id)
for key in student_info:
print(key + ':', student_info[key])
print("\n")
elif authorized_input == 4:
print("option 4")
elif authorized_input == 5:
quit()
else:
print("access denied")
Change this to
students[student_name] = {student_id, student_age, student_grade}
this
students[student_name] = {'id': student_id, 'age': student_age, 'grade': student_grade}
You are expecting each item in students
to be a dictionary itself (so students
is a dictionary of dictionaries). To do that, you need to change
students[student_name] = {student_id, student_age, student_grade}
to something like
students[student_name] = {
'id': student_id,
'age': student_age,
'grade': student_grade
}
As currently written, you are creating a set
{student_id, student_age, student_grade}
instead of a dict
.
You need to give the proper keys to each value in the dict:
students ={
"Alice":{"id": "1", "age":26, "grade":"A"},
"Bob":{"id": "2", "age":34, "grade":"C"},
"Jimbo":{"id": "3", "age":12, "grade":"B"},
"Karen":{"id": "4", "age":33, "grade":"D"},
"Keith":{"id": "5", "age":53, "grade":"F"}
}
student_name = 'DirtyBit'
student_id = '12345'
student_age = 22
student_grade = 'A'
students[student_name] = {'id': student_id, 'age': student_age, 'grade': student_grade}
print(students)
Change this (which is a set
):
students[student_name] = {student_id, student_age, student_grade}
to this (which is a key-value
paired dict
):
students[student_name] = {'id': student_id, 'age': student_age, 'grade': student_grade}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.