简体   繁体   中英

How to create empty dictionary in python and insert data into it from while running the program?

I want to ask how can I create an empty dictionary in Python and then fill it with data while running the program? I have written the logic of my program and what it needs to do, but i don't know how to store the values into dictionary and also if there are repetitive inputs, like for the same student, just add the new values without overwriting. This is the logic of my program. It is a gradebook basically.

Code:

#students{}
ime = input("Vnesete ime na studentot: ")
prezime = input("Vnesete prezime na studentot: ")
indeks = int(input("Vnesete broj na indeks na studentot: "))
predmet = input("Vnesete predmet za studentot: ")
teorijapoeni = float(input("Vnesete poeni osvoeni od teorija na studentot: "))
zadacipoeni = float(input("Vnesete poeni osvoeni od zadaci na studentot: "))
labpoeni = float(input("Vnesete poeni osvoeni od laboratoriski vezbi na studentot: "))
vkupnopoeni = (labpoeni + teorijapoeni + zadacipoeni)
ocenka = None
if ((vkupnopoeni>=0) and (vkupnopoeni<50)):
ocenka = 5
elif ((vkupnopoeni>50) and (vkupnopoeni<60)):
ocenka = 6
elif ((vkupnopoeni>=60) and (vkupnopoeni<70)):
ocenka = 7
elif ((vkupnopoeni>=70) and (vkupnopoeni<80)):
ocenka = 8
elif ((vkupnopoeni>=80) and (vkupnopoeni<90)):
ocenka = 9
elif ((vkupnopoeni>=90) and (vkupnopoeni <=100)):
ocenka = 10
print("Student:" ,ime, prezime)
print(predmet, ocenka)

The program needs to return students name in the first row and in the following rows, it needs to return the subjects he has, and the grades he earned.

Ie. Student: Alice Smith Artificial Intelligence 8 Robotics 9

You can use pythons dictionaries like this:

students = {}
students["some random name"] = 50
# now students is {"some random name": 50}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM