-3
students = {}
name = input("Give me the student name(key): ")
grade = input("What is their Grade(value): ")

'(put in to dictionary key=name and value = grade print all students and their grades from dictionary"

Lavo
  • 1
  • 3
  • i'm not able to add to dictionary from input. can you give me a little example of adding key and value from input? – Lavo Oct 07 '18 at 14:50
  • Why don't you Google search for how to use dictionaries? As it is, your question is very unclear. – roganjosh Oct 07 '18 at 14:51
  • @Lavo just try `student.update({ name : grade})` as there may be an issue resolving the key (name). – Mohanavel T Jan 30 '19 at 07:01

2 Answers2

0
students = {}
name = input("Give me the student name(key): ")
grade = input("What is their Grade(value): ")
students[name] = grade

You can just use students[key] = value

Rohit-Pandey
  • 2,039
  • 17
  • 24
  • `students[name] = grade` could have been added as a comment. Fixing the basics of the OP's understanding here neither helps them to learn how to research their problem, nor benefits other people. – roganjosh Oct 07 '18 at 14:53
  • That is what I did before. retried. I get msg 'name' not defined after I enter the student name. I know name would be the key and the grade would be the value. and looping around should let me add another entry. but I can get the first entry in? I tried you example to be sure. – Lavo Oct 07 '18 at 15:12
0

Below code should do your job. For details on how to add to dictionary you can refer the links shared above or go through the python documentation for it.

students = {}
name = input("Give me the student name(key): ")
grade = input("What is their Grade(value): ")
students[name] = grade
print(students)
mayurmadnani
  • 189
  • 4
  • that's also how I tried it. I also, ran your code too mayurmadnani. I defined my dictionary students ={}. Am I missing a definition other than the code you listed. of course im running on python 2.7. I can pre fill my dictionary by hard coding it ,but not by input. – Lavo Oct 07 '18 at 15:38
  • what challenge are you facing? the above snippet works. See the output below – mayurmadnani Oct 07 '18 at 15:43
  • Give me the student name(key): me What is their Grade(value): 2 {'me': '2'} – mayurmadnani Oct 07 '18 at 15:44
  • I found the problem. it needs quotes when you type in the key at a input prompt... i.e 'student1'. but how will I get the quotes around the input field . Maybe I can format it – Lavo Oct 07 '18 at 17:08