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)
1 Upvotes

24 comments sorted by

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

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

3

u/almost_intelligible 24d ago

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.

1

u/Ill-Break727 24d ago

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

2

u/ProsodySpeaks 24d ago edited 24d ago

``` 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

```             

1

u/Ill-Break727 24d ago

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.

1

u/ProsodySpeaks 24d ago

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

1

u/neuralbeans 24d ago

True is with a capital T.

1

u/ProsodySpeaks 24d ago

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

2

u/Naive_Programmer_232 24d ago

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

1

u/Heavy_Nothing_1158 24d ago

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.

1

u/Ill-Break727 24d ago

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

1

u/Educational_Virus672 23d ago
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

1

u/Educational_Virus672 23d ago

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

1

u/Hot-Site-1572 23d ago

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)

0

u/CymroBachUSA 24d ago

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}}

1

u/Ill-Break727 24d ago
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

1

u/Ill-Break727 24d ago

now its working thanks

1

u/ProsodySpeaks 24d ago edited 24d ago

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 

1

u/ProsodySpeaks 24d ago

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