Python打印超出范围?

时间:2016-12-08 09:17:08

标签: python parameter-passing indentation

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

我在这方面遇到了问题。我想让用户创建一个课程。一旦他添加了我想将其发送到选项3的信息以显示它。

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)

因此,一旦用户创建了新课程,并且他选择查看它我希望它打印到add_Course选项中输入的信息。     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)

在您的第二个示例中,您未声明myCourse

myCourse = Course.Course(name,number,units,instructor)

缺失。

答案 1 :(得分:0)

在Python中,每个函数都有自己的命名空间。如果在函数中创建一个变量,它只会在函数返回之前存在,并且只能在函数内部访问。

要解决您的问题,您必须调用第一个函数并具有返回值。然后用该值调用第二个函数:

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 view_Course(myCourse):

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

course = add_Course()
view_Course(course)