将此字典传递给函数

时间:2019-03-27 02:03:34

标签: python function dictionary

我需要以某种方式访问​​类字典并使用removeClass()函数将其删除,但不确定如何在其中传递类。选择了数字2选项后,该程序应该从字典中删除该类。

我不确定该怎么做。

def prompt():
    print('MENU')
    print('1 --> Add classes.')
    print('2 --> Remove classes.')
    print('3 --> View course details.')
    print('4 --> View schedule.')
    print('5 --> Quit.')
    option = int(input('What would you like to choose? '))
    while option < 1 or option > 5:
        option = int(input('Please enter a valid numerical option: '))
    return option

def checkOption(option):
    if option == 1:
        courseCount = 0
        courseCount = courseCounter()
        classes = addClasses(courseCount)
    elif option == 2:
        removeClasses(courses)
    elif option == 3:
        viewCourseDetails()
    elif option == 4:
        viewSchedule()


def courseCounter():
    courseCount = input('Enter a numerical value of courses only (up to 4): ')
    while courseCount.isnumeric() == False:
          courseCount = input('Enter a NUMERICAL value of courses only (up to 4): ')
    return int(courseCount)

def addClasses(courseCount):
    classes = {}
    i = 1
    while i <= courseCount:
        courseName = input('Enter a course name: ')
        classes[courseName] = {}
        classes[courseName]['Room Number'] = input('Enter a room number: ')
        classes[courseName]['Instructor'] = input('Enter a instructor: ')
        classes[courseName]['Meeting time'] = input('Enter a meeting time: ')
        i = i + 1
    return classes

def removeClasses(*****):
    courseName = input('Enter the class you would like to remove: ')
    if ***** in classes:
        del classes[courseName]
    print(classes)



def main():
    option = prompt()
    checkOption(option)
    while option > 1 or option < 5:
        if option == 5:
            break
        option = prompt()
        checkOption(option)
main()

2 个答案:

答案 0 :(得分:0)

通过globals()变量,以便您可以跨函数传递它:

def prompt():
    print('MENU')
    print('1 --> Add classes.')
    print('2 --> Remove classes.')
    print('3 --> View course details.')
    print('4 --> View schedule.')
    print('5 --> Quit.')
    option = int(input('What would you like to choose? '))
    while option < 1 or option > 5:
        option = int(input('Please enter a valid numerical option: '))
    return option

def checkOption(option):
    if option == 1:
        courseCount = 0
        courseCount = courseCounter()
        classes = addClasses(courseCount)
    elif option == 2 :
        if 'classes' in globals() :
            removeClasses(globals()['classes'])
        else :
            print("Insert a class before!")
            checkOption(1)
    elif option == 3:
        viewCourseDetails()
    elif option == 4:
        viewSchedule()

def courseCounter():
    courseCount = input('Enter a numerical value of courses only (up to 4): ')
    while courseCount.isnumeric() == False:
          courseCount = input('Enter a NUMERICAL value of courses only (up to 4): ')
    return int(courseCount)

def addClasses(courseCount):
    classes = {}
    i = 1
    while i <= courseCount:
        courseName = input('Enter a course name: ')
        classes[courseName] = {}
        classes[courseName]['Room Number'] = input('Enter a room number: ')
        classes[courseName]['Instructor'] = input('Enter a instructor: ')
        classes[courseName]['Meeting time'] = input('Enter a meeting time: ')
        i = i + 1
    globals()[ 'classes' ] = classes
    return classes

def removeClasses(classes):
    courseName = input('Enter the class you would like to remove: ')
    if courseName in classes:
        del classes[courseName]
    print(classes)



def main():
    option = prompt()
    checkOption(option)
    while option > 1 or option < 5:
        if option == 5:
            break
        option = prompt()
        checkOption(option)
main()

这在我的机器上有效。这还将检查类的存在或重定向用户以定义类。

注意globals()的行为(例如Why are global variables evil?)。否则,您可以定义一个对象(一个空字典)并将其传递给函数,填充它或检查是否为空,这可能更安全。

答案 1 :(得分:0)

我能想到的最简单的方法如下。我修改了您的2个函数,并将“类”设置为全局。

classes = {}


def checkOption(option):
    global classes
    if option == 1:
        courseCount = courseCounter()
        classes = addClasses(courseCount)
    elif option == 2:
        removeClasses()
    elif option == 3:
        viewCourseDetails()
    elif option == 4:
        viewSchedule()


def removeClasses():
    courseName = input('Enter the class you would like to remove: ')
    if courseName in classes:
        del classes[courseName]
    print(classes)

希望对您有帮助。