Python打印超出范围?

时间:2016-12-08 11:06:12

标签: python python-3.x

所以这是我的计划。当用户选择选项1时,他应该能够创建课程。在他创建之后,他应该能够选择选项3,并显示他在选项1中输入的内容。但我的问题是,当它创建的课程,并且想要显示它时,我得到了这个错误。 * NameError:名称'myCourse'未定义*

import Course
import Person
import Student


CREATE = 1
ADD_STUDENTS = 2
COURSE_INFO = 3
ADD_SCORES = 4
QUIT = 5


def main():

    choice = 0

    while choice != QUIT:
        choice = get_menu_choice()

        if choice == CREATE:
            add_Course()
        elif choice == ADD_STUDENTS:
            add_Student()
        elif choice == COURSE_INFO:
            view_Course()
        elif choice == ADD_SCORES:
            add_Scores()
        elif choice == QUIT:
            quit()



def get_menu_choice():
    print()
    print(" Welcome to CourseAware's Faculty Menu")
    print('---------------------------')
    print('1. Create Course ')
    print('2. Add Students to a Course')
    print('3. View Course Information')
    print('4. Add Test scores to a Course')
    print('5. Exit')
    print()

    choice = int(input('Enter your choice: '))

    while choice < CREATE or choice > QUIT:
        choice = int(input('Enter a valid choice: '))

    return choice




def add_Course():

    name = input("Enter course name: ")
    number = input("Enter course number: ")
    units = input("Enter courses units: ")
    instructor = input("Enter courses Instructor: ")
    myCourse = Course.Course(name,number,units,instructor)

    print("Courses' Name: ",myCourse.getName())
    print("Courses' Number: ",myCourse.getNumber())
    print("Courses' Units: ",myCourse.getUnits())
    print("Courses' Instructor: ",myCourse.getInstructor())
    print("Course Added!")
    return myCourse

def add_Student():
    Name = input("Enter First and Last Name of Sudent: ")
    Status = input("Enter Status of Stdent: ")
    GPA = input("Enter Students GPA: ")
    newStudent = Student.Student(Name, Status, GPA )

    print("Student' First Name: ",newStudent.getName())
    print("Student' Status: ",newStudent.getStatus())
    print("Student' Instructor: ",newStudent.getGPA())


    infile = open('student.txt','a')
    infile.write('\n')
    infile.write(newStudent)

错误就在这里

def view_Course():



    print("Courses' Name: ",myCourse.getName())
    print("Courses' Number: ",myCourse.getNumber())
    print("Courses' Units: ",myCourse.getUnits())
    print("Courses' Instructor: ",myCourse.getInstructor())

main()

提前致谢!

2 个答案:

答案 0 :(得分:0)

您收到此错误是因为view_Course函数不了解您在myCourse函数中创建的add_Course对象。虽然add_Course函数会将myCourse返回到main函数,但myCourse中的任何位置都不会使用返回的main值。

您当然可以将myCourse移至全局范围级别,但这是一种不好的做法。

最好将myCourse范围保持在main作为参数的view_Course函数,例如,像这样:

myCourse = None;
if choice == CREATE:
        myCourse = add_Course(myCourse)
...
elif choice == COURSE_INFO:
        view_Course(myCourse)

答案 1 :(得分:0)

有两种方法可以解决它。

一种方法是使整个文件中的MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName socketBindingMBean = new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=http"); Integer port = (Integer) mBeanServer.getAttribute(socketBindingMBean, "boundPort")); 全局

myCourse

2路是传输变量,如下所示:

def view_Course(myCourse):
    global myCourse

在我看来,第二种方式更好。