满足条件时防止循环退出的自定义类

时间:2020-10-14 14:48:39

标签: python while-loop break python-3.8

当用户输入no时,我试图打破外部while循环(使用大写字母;我使用上层函数)。当前,当用户输入“ no”时,将再次运行while循环,如果再次输入“ no”,则会退出while循环。

问题与“主程序”上方定义的“课程”类有关。为了解决这个问题,我创建了一个单独的“主程序”来初始化Course类。

我对于为什么会发生这种情况感到困惑,并且想知道是否可以提供一些澄清。

在Python 3.8.6上运行

class Course(object):
    # init method or constructor that creates a Course object
    def __init__(self, name, number, section, term_year, students):
        self.name = name
        self.number = number
        self.section = section
        self.term_year = term_year
        self.students = students
    
    #methods to edit specific course object attributes:    
    #edit the course name   
    def edit_name(self, name):
        self.name = name
    #edit the course number
    def edit_number(self, number):
        self.number = number
    #edit the course section
    def edit_section(self, section):
        self.section = section
    #edit the course term and year
    def edit_term_year(self, term_year):
        self.term_year = term_year
    #edit the course number of students
    def edit_students(self, students):
        self.students = students
   
    #methods to display the course object attributes
    #display all attributes
    def display_course(self):
        return( self.name+" "+ self.number + " " + self.section + " " +self.term_year+ " " + str(self.students))
    #display the course name
    def display_course_name(self):
        return( self.name)
    #display the course number
    def display_course_number(self):
        return( self.number)
    #display the course section
    def display_course_section(self):
        return( self.section)
    #display the course term and year
    def display_course_term_year(self):
        return( self.term_year)
    #display the number of students
    def display_course_students(self):
        return( self.students)
    #function to delete the course object
    def delete_course(self):
        del self

from Course import Course

Courses = []
loop = True
while loop:
    print(loop)
    #store the number of courses the user would like to add
    number = int(input("How many courses would you like to add? "))
    #iterate through the number of courses the user would like to enter
    if number > 0:
        for x in range(number):
            print(x)
            #store user input for each course
            name = input("Enter course name: ")
            number = input("Enter course number: ")
            section = input("Enter course section: ")
            term_year = input("Enter course term and year: ")
            students = input("Enter number of student for course: ")
            print("\n")
            
            #store each course into an array
            Courses.append([name,number,section,term_year,students])
    
    answer = input("Would you like to add anymore courses?").upper()
    print(answer)
    while True:
        if answer in ('YES','NO'):
            break
        else:
            answer = input('Incorrect option. Type "YES" to add more courses or "NO" to continue": ').upper()
    
    if answer == 'NO':
        loop = False
        break
    print(loop)
print(Courses)

当前输出如下:

True
How many courses would you like to add? 0
Would you like to add anymore courses?no
NO
[]
True
How many courses would you like to add? 0
Would you like to add anymore courses?no
NO
[]

1 个答案:

答案 0 :(得分:0)

在复制粘贴代码并使用类似的python版本后,我无法重现您的问题。

类似于@balderman提到的那样,您在哪里打印了一个空列表?我认为您的答案中缺少一些代码行。

$ python test.py
True
How many courses would you like to add? 0
Would you like to add anymore courses?NO
NO