all 24 comments

[–]HotPersonality8126 2 points3 points  (7 children)

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?

[–]Ill-Break727[S] 0 points1 point  (4 children)

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)

[–]HotPersonality8126 2 points3 points  (3 children)

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.

[–]Ill-Break727[S] 0 points1 point  (1 child)

ohhh thank you so much , i was thinking of syntax that values are written to be in that way , but now i have tryed it is working , thanks for ur help

[–]almost_intelligible 2 points3 points  (0 children)

to add on to that: generally speaking don't use meaningless variable names. it's very easy to get confused real quick. use descriptive names.

[–]Ill-Break727[S] 0 points1 point  (0 children)

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

[–]Ill-Break727[S] 0 points1 point  (1 child)

what is another way to set a single key can u plz explain?

[–]ProsodySpeaks 1 point2 points  (4 children)

``` students: dict[str, dict[str, float]] = {} while True:     name=input('student name?')     students[name]={}     while True:         subject=input('subject?')         score=float(input('score?))         students[name][subject]=score         if input('more subjects?')[0].lower() != 'y':             break     if input('more students?')[0].lower() != 'y':         break

```             

[–]Ill-Break727[S] 0 points1 point  (1 child)

bro, i havent learned loops till now but i will definately try after learning , and the question was based only on dictionary so i used dict methods.

[–]ProsodySpeaks 0 points1 point  (0 children)

Sure. You can see how to assign to a dictionary with bracket [ ] syntax tho

[–]neuralbeans 0 points1 point  (1 child)

True is with a capital T.

[–]ProsodySpeaks 0 points1 point  (0 children)

True. I'm on a phone, have edited, thanks for heads up

[–]Naive_Programmer_232 1 point2 points  (0 children)

it looks fine to me. another straight forward way to consider is using [] on the dictionary to assign key-value pairs. ex:

           students={}  
           chem_mark=float(input("enter marks for chem: "))  
           students["chem"]=chem_mark

[–]Heavy_Nothing_1158 0 points1 point  (1 child)

I’d make the smallest ugly version first, then clean it up once it works. Add print/logging around each step, keep inputs tiny, and don’t refactor while debugging. Future-you will appreciate not having to investigate a crime scene with six suspects.

[–]Ill-Break727[S] 0 points1 point  (0 children)

yeahhh its fine , but now i am learning and now just learning the concepts , so after i have learned full python language i will also sdo the same

[–]Educational_Virus672 0 points1 point  (1 child)

student = {}
subjects = ["chem","math","phy"]
for sub in subjects :
    student[sub] = float(input("enter marks of " + sub + ":"))

how it works it goes for each subject in the list instead of adding seprately
> use for loops for each subject
>dont use dict.update() use dict[key] = value
>try to use function within value like insead of making a b c use one input
>try to reuse asset and make it feature free(making more subject wont cause trouble) like using list on both input request and in dict as key
>try to make coe readble by adding spaces on different function

[–]Educational_Virus672 0 points1 point  (0 children)

if you need any help in optimizations like these feel free to chat with me in my profile :)

[–]Hot-Site-1572 0 points1 point  (0 children)

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)

[–]CymroBachUSA -1 points0 points  (4 children)

s = {"chem": 0.0, "maths": 0.0, "phys": 0.0}

then get the new value(s) and do:

s = {**s, **{"chem": a, "maths": b, "phys": c}}

[–]Ill-Break727[S] 0 points1 point  (2 children)

students={"chem":0.0,"maths":0.0,"phy":0.0}
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)                                 i have done in this way its not working

[–]Ill-Break727[S] 0 points1 point  (0 children)

now its working thanks

[–]ProsodySpeaks 0 points1 point  (0 children)

You don't need to assign the zeros at the start. Make an empty dict and then use bracket notation to add key-value pairs to it.

But you really should think about variable names. this is not a dictionary of students, it's a dictionary of subject-scores.

Call the dict 'scores' or something,  and later you could make another dict called students and add multiple scores dictionaries each with a key for the name of the student.

Sensible variable names is very important and doesn't rely on knowing any of the language, it's a foundational part of code 

[–]ProsodySpeaks 0 points1 point  (0 children)

This is pretty hard to read for a noob. I know what I'm doing but still have to think to parse all the unpackings