I'm currently practicing on python language and I try to make a sample program that will Add, Show, Delete and Update students info.. But I have this error "global name 'b' is not defined" on the 'Show()', 'Delete()' & 'Update()'.. please help me me guys..Thank you..

x = 1
y = 1
z = 1
q = 1
f = {}


print('                          W E L C O M E')
print('                      Please select a choice')
print('*******************************************************************')
print('Enter "1" to add student[s]')
print('Enter "2" to show the list of student[s]')
print('Enter "3" to update student[s]')
print('Enter "4" to delete student[s]')
print('*******************************************************************')
print('Please type the word "start()" to launch/re-launch the application. ')

def start():
    choice = input('Enter choice number:')
    print(' ')
    if choice == '1':
        Add()
    elif choice == '2':
        Show()
    elif choice == '3':
        Update()
    elif choice == '4':
        Delete()
    else:
        print('Invalid choice!')


def Add():
    while x ==1:
        Add_student = input('Add Student[s]? Y/N: ')
        if Add_student =='y' or Add_student == 'Y':
             a = input('Student ID: ')
             b = input('Name: ')
             c = input('Age: ')
             d = input('Course: ')
             e = input('Year: ')
             f.update({a:{'Name':b, 'Age':c, 'Course':d, 'Year':e}})
        else:
            break

def Show():
    while y ==1:
        Show_student = input('Show student[s] list?Y/N:')
        if Show_student == 'y' or Show_student == 'Y':
            w = input('Enter ID:')
            print('Name:',b)
            print('Age:',c)
            print('Course:',d)
            print('Year:',e)
            f.update({w:{'Name':b, 'Age':c, 'Course':d, 'Year':e}})
        else:
            break

def Update():
    while z ==1:
        Update_student = input('Update student[s] info?Y/N: ')
        if Update_student == 'Y' or Update_student == 'y':
            del_stud = input('Enter Student ID: ')
            del(f[del_stud])
            new_name = input('Name: ')
            new_age = input('Age: ')
            new_course = input('Course: ')
            new_year = input('Year: ')
            f.update({del_stud:{'Name':new_name, 'Age':new_age, 'Course':new_course, 'Year':new_year}})
            print(f)
            print(b, 'has been updated!')
        else:
            break

def Delete():
    while q==1:
        Delete_student = input('Delete Student[s]?Y/N: ')
        if Delete_student == 'Y' or Delete_student == 'y':
            del_stud_ID = input('Student ID: ')
            del(f[del_stud_ID])
            print(f)
            print(b, 'has been deleted!')
        else:
            break

Recommended Answers

All 3 Replies

What is the purpose of the variable and the while loop?

You define the variable b inside of these private methods, so it doesn't exit outside of the methods. You define it in add, but it is not passed to the other methods. The way you are setting this up, it's suitable for object oriented programming, meaning you should use classes. I'll try to provide an example later.

Pass the dictionary to all of the functions, as that is what you are operating on. Something like

def add_student(student_dict):

    ## the while() loop is only necessary for the Yes/No input
    add_student="x"
    while add_student not in ["Y", "N"]:
        add_student = input('Add Student[s]? Y/N: ')
        add_student = add_student.upper()

    if add_student == 'Y':
        a = input('Student ID: ')
        b = input('Name: ')
        c = input('Age: ')
        d = input('Course: ')
        e = input('Year: ')
        if a not in student_dict:
            student_dict[a]={'Name':b, 'Age':c, 'Course':d, 'Year':e}
        else:
            print "student", a, "is already on file"

    return student_dict

##---------- test the add
student_dict = {}
for ctr in range(3):                          ## add 3 students
    student_dict = add_student(student_dict)
print student_dict

## etc for all functions.  For example
student_dict = Del(student_dict)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.