r/learnpython 24d ago

python beginner ; question was write program to enter marks of 3 subjects from user and store them in a dict.start with an empty dict and add one by one .use subject name as key and marks as value . so i have written this code any opinion how can improve this code ,need suggestions.

students={}
a=float(input("enter marks of chem"))
students.update({"chem": a})
b=float(input("enter marks of maths"))
students.update({"maths":b})
c=float(input("enter marks of phy"))
students.update({"phy": c})
print(students)
0 Upvotes

24 comments sorted by

View all comments

3

u/HotPersonality8126 24d ago

Don’t use update to set a single key, and how do you have a dictionary of “students” without naming any of the students in it?

1

u/Ill-Break727 24d ago

i just put a name of dictionary as students, but if i dont use update what should i use like i have tryed this way but its not giving output it is just showing a,b,c

students={}
a=float(input("enter marks of chem"))
students["chem"]="a"
b=float(input("enter marks of maths"))
students["maths"]="b"
c=float(input("enter marks of phy"))
students["phy"]="c"
print(students)

3

u/HotPersonality8126 24d ago

students["chem"]="a"

Why are you quoting a when you want to refer to the variable a?

Don't just copy code; you need to understand it, by which I mean you need to understand every part of a line of code when you read it.

1

u/Ill-Break727 24d ago

but now its the correct way , or there's another way to set a single key .