更新其他功能中的字典

时间:2019-10-31 05:29:48

标签: python function dictionary

我正在尝试将内容添加到字典中,但是由于字典在另一个函数中,因此当我使用addCourses()函数时它们永远不会更新。

当调用addCourses()函数时,我将包含字典的函数添加到其透视图位置中,但它不会更新原始字典,因此当我尝试使用getCourseInfo作为新课程时,它就不存在。

def courseRooms():
    rooms = {'CS101': '3004', 'CS102': '4501', 'CS103': '6755', 'NT110': '1244', 'CM241': '1411'}
    return rooms


def courseInstructors():
    instructors = {'CS101': 'Haynes', 'CS102': 'Alvarado', 'CS103': 'Rich', 'NT110': 'Rich', 'CM241': 'Lee'}
    return instructors


def courseTimes():
    times = {'CS101': '8:00am', 'CS102': '9:00am', 'CS103': '10:00am', 'NT110': '11:00am', 'CM241': '1:00pm'}
    return times


def getCourseInfo(rooms, instructors, times, course):
    if course in rooms:
        print('Course:\t\t', course)
        print('Room:\t\t', rooms.get(course))
        print('Instructor:\t', instructors.get(course))
        print('Time:\t\t', times.get(course))
    else:
        print("Course number not found. Please try again.")


def addCourse(room, instructor, time):
    course = input("Please enter the course number you are trying to add: ").upper()
    room[course] = 'null'
    roomNumber = input("Please enter the room number for that course: ")
    room[course] = roomNumber
    teacher = input("Please enter the instructor for that course: ").title()
    instructor[course] = teacher
    meetingTime = input("Please enter the meeting time for that course: ")
    time[course] = meetingTime
    print("Thank you. That course has been added.")

是根本不更新字典,还是只是不更新​​原始字典?它不会给我和错误,但不能按预期工作。

2 个答案:

答案 0 :(得分:1)

rooms是函数courseRooms()中的局部变量,由于作用域,您不能从函数中访问它。实际上,您可以在功能之外分配它,以便其他功能可以访问它。例如,

rooms = {'CS101': '3004', 'CS102': '4501', 'CS103': '6755', 'NT110': '1244', 'CM241': '1411'}
instructors = {'CS101': 'Haynes', 'CS102': 'Alvarado', 'CS103': 'Rich', 'NT110': 'Rich', 'CM241': 'Lee'}
times = {'CS101': '8:00am', 'CS102': '9:00am', 'CS103': '10:00am', 'NT110': '11:00am', 'CM241': '1:00pm'}

def getCourseInfo(course):
    if course in rooms:
        print('Course:\t\t', course)
        print('Room:\t\t', rooms.get(course))
        print('Instructor:\t', instructors.get(course))
        print('Time:\t\t', times.get(course))
    else:
        print("Course number not found. Please try again.")


def addCourse():
    course = input("Please enter the course number you are trying to add: ").upper()
    room[course] = 'null'
    roomNumber = input("Please enter the room number for that course: ")
    room[course] = roomNumber
    teacher = input("Please enter the instructor for that course: ").title()
    instructor[course] = teacher
    meetingTime = input("Please enter the meeting time for that course: ")
    time[course] = meetingTime
    print("Thank you. That course has been added.")

答案 1 :(得分:0)

因此,要做这项工作所需要做的就是将字典添加到我的main()函数中,然后使用它们将它们作为参数传递给其他函数。本质上,我必须将它们传递给使用字典的每个函数,但是在我的示例中,我仅显示了getCourseInfo()函数。
示例:

def main():
    try:
        rooms = {'CS101': '3004', 'CS102': '4501', 'CS103': '6755', 'NT110': '1244', 'CM241': '1411'}
        instructors = {'CS101': 'Haynes', 'CS102': 'Alvarado', 'CS103': 'Rich', 'NT110': 'Rich', 'CM241': 'Lee'}
        times = {'CS101': '8:00am', 'CS102': '9:00am', 'CS103': '10:00am', 'NT110': '11:00am', 'CM241': '1:00pm'}

        choice = file.mainMenu()
        while choice != 3:
            if choice == '1':
                course = input("What course number would you like to see info for?: ").upper()
                file.getCourseInfo(rooms, instructors, times, course)

            elif choice == '2':
                course, roomNumber, teacher, time = file.addCourse(rooms, instructors, times)
                rooms[course] = roomNumber
                instructors[course] = teacher
                times[course] = time
                print("Thank you. That course has been added.")

            else:
                print("Thank you. Exiting...")
                exit()

            choice = file.mainMenu()

    except Exception as err:
        print('Error in main: ', err)


def getCourseInfo(rooms, instructors, times, course):
    if course in rooms:
        print('Course:\t\t', course)
        print('Room:\t\t', rooms.get(course))
        print('Instructor:\t', instructors.get(course))
        print('Time:\t\t', times.get(course))
    else:
        print("Course number not found. Please try again.")