在python中创建学生数据库

时间:2017-02-28 09:10:04

标签: python python-2.7 list dictionary

我是python的初学者。我在python中为student创建了一个数据库。我无法获得dict中所有迭代的输出。

我的源代码: -

n = int(raw_input("Please enter number of students:"))

student_data = ['stud_name', 'stud_rollno', 'mark1', 'mark2','mark3','total', 'average'] for i in range(0,n):

    stud_name=raw_input('Enter the name of student: ')
    print stud_name


    stud_rollno=input('Enter the roll number of student: ')
    print stud_rollno


    mark1=input('Enter the marks in subject 1: ')
    print mark1

    mark2=input('Enter the marks in subject 2: ')
    print mark2

    mark3=input('Enter the marks in subject 3: ')
    print mark3

    total=(mark1+mark2+mark3)
    print"Total is: ", total

    average=total/3
    print "Average is :", average

dict = {'Name': stud_name, 'Rollno':stud_rollno, 'Mark1':mark1, 'Mark2':mark2,'Mark3':mark3, 'Total':total, 'Average':average} print "dict['Name']: ", dict['Name'] print "dict['Rollno']: ", dict['Rollno'] print "dict['Mark1']: ", dict['Mark1'] print "dict['Mark2']: ", dict['Mark2'] print "dict['Mark3']: ", dict['Mark3'] print "dict['Total']: ", dict['Total'] print "dict['Average']: ", dict['Average']

如果我不给学生2,我只能获得第二个数据库的词典,而不是第一个数据库。

我得到的输出

Please enter number of students:2
Enter the name of student: a
a
Enter the roll number of student:1
1
Enter the marks in subject 1:1
1
Enter the marks in subject 2:1
1
Enter the marks in subject 3:1
1
Total is:3
Average is :1
Enter the name of student:b
b
Enter the roll number of student:2
2
Enter the marks in subject 1:2
2
Enter the marks in subject 2:2
2
Enter the marks in subject 3:2
2
Total is:6
Average is :2
dict['Name']:b
dict['Rollno']:2
dict['Mark1']:2
dict['Mark2']:2
dict['Mark3']:2
dict['Total']:6
dict['Average']:2

2 个答案:

答案 0 :(得分:2)

您当前的代码会覆盖每dicti。此外,您不应使用名称dict,因为这会导致与Python中的数据类型dict混淆。

一个解决方案是使用字典列表,如下所示:

n = int(raw_input("Please enter number of students:"))

all_students = []

for i in range(0, n):
    stud_name = raw_input('Enter the name of student: ')
    print stud_name

    stud_rollno = input('Enter the roll number of student: ')
    print stud_rollno

    mark1 = input('Enter the marks in subject 1: ')
    print mark1

    mark2 = input('Enter the marks in subject 2: ')
    print mark2

    mark3 = input('Enter the marks in subject 3: ')
    print mark3

    total = (mark1 + mark2 + mark3)
    print"Total is: ", total

    average = total / 3
    print "Average is :", average

    all_students.append({
                            'Name': stud_name,
                            'Rollno': stud_rollno,
                            'Mark1': mark1,
                            'Mark2': mark2,
                            'Mark3': mark3,
                            'Total': total,
                            'Average': average
                            })

for student in all_students:
    print '\n'
    for key, value in student.items():
        print '{0}: {1}'.format(key, value)

通过将每个新学生的答案添加到列表的末尾,我们依次跟踪所有这些答案。最后,我们使用字典中存储的keysvalues打印出来。这将打印出您的输入:

Name: a
Rollno: 1
Average: 1
Mark1: 1
Mark2: 1
Mark3: 1
Total: 3


Name: b
Rollno: 2
Average: 2
Mark1: 2
Mark2: 2
Mark3: 2
Total: 6

答案 1 :(得分:0)

stud = {}
mrk = []
print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")
print("ENTER STUDENT INFORMATION ------------------------ ")
while True :
    rno = int(input("enter roll no.. :: -- "))
    if rno == 0 :
        break
    else:
         for i in range(3):
             print("enter marks  ",i+1," :: --  ",end = " ")
             n = int(input())
             mrk.append(n)
    stud[rno] = mrk
    mrk = []
print("Records are ------ ",stud)
print("\nRollNo\t Mark1\t Mark2\t Mark3\t Total")
tot = 0
for r in stud:
    print(r,"\t",end=" ")
    for m in stud[r]:
        tot = tot + m
        print(m,"\t",end=" ")
    print(tot)
    tot = 0