anonymous anonymous -4 years ago 202
Python Question

create student database in python

I'm a beginner in python. I have created a database for student in python. I'm not able to get the output for all iterations in dict.

my source code:-

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']


if i give no of students as 2, I'm getting dict for 2nd database only and not for 1st one too.

output that i got

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

Answer Source

Your current code overwrites dict for every i. In addition, you shouldn't use the name dict as this causes confusion with the data type dict in Python.

One solution for you is to use a list of dictionaries, something like the following:

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)

By adding the responses for each new student onto the end of the list, we track all of them in turn. Finally, we print them out using the keys and values stored in the dictionary. This prints, with your inputs:

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download