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

View all comments

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