簡體   English   中英

嘗試調試 Peer Tutoring Simulation 程序的邏輯錯誤

[英]Trying to debug a Peer Tutoring Simulation program's logical error

大家好,基本上,我試圖完成一項編程任務,但似乎我需要一些備份。

這是我現在擁有的 3 個代碼文件:

class Tutor:

  def __init__(self, name, grade, subjects, days, hours = 0.0):
      self.name = name
      self.grade = grade
      self.subjects = subjects
      self.days = days
      self.hours = 0.0

  def get_name(self):
    return self.name
    
  def get_grade(self):
    return self.grade
  
  def get_subjects(self):
    return self.subjects
  
  def get_days(self):
    return self.days
  
  def get_hours(self):
    return self.hours
  
  def add_hours(self, hours = 0.0):
    self.hours += hours
  
  def __str__(self):
    string = ""
    string += self.name
    string += "\nGrade:", self.grade
    string += "\nSubjects:", self.subjects
    string += "\nDays:", self.days
    string += "\nHours:", self.hours
    return string

class Peer_tutoring:
    """A class to represent and manage NNHS peer tutors"""
    def __init__(self, tuts = None):
        if tuts == None:
            tuts = []
        # a list of Tutor objects, representing all the active peer tutors
        self.tutors = tuts
        self.teacher = "Mrs. Moore"
    
    def add_tutor(self, new_tutor):
        """adds a new Tutor object to the tutor list
        param: new_tutor - a Tutor object"""
        self.tutors.append(new_tutor)

    def remove_tutor(self, tutor):
        """removes a tutor from the tutor list, by name
        param: tutor - a String name of a Tutor"""
        for tut in self.tutors:
            if tut.get_name() == tutor:
                i = self.tutors.index(tut)
                return self.tutors.pop(i)
    
    def find_tutor_subj(self, subj):
        """returns a list of all tutors that tutor in a certain subject
        param: subj - a String subject to search for"""
        tuts = []
        for tut in self.tutors:
            subs = tut.get_subjects()
            if subj in subs:
                    tuts.append(tut)
        return tuts
    
    def find_tutor_day(self, day):
        """returns a list of all tutors that tutor a certain day
        param: day - a String day to search for"""
        tuts = []
        for tut in self.tutors:
            d = tut.get_days()
            if day in d:
                tuts.append(tut)
        return tuts

    def add_hours(self, name, hours):
        """Adds hours to a Tutor object.
        param: name - a String name of a tutor
        param: hours - an int or float number of hours to add"""
        for i in range(len(self.tutors)):
            if self.tutors[i].get_name() == name:
                self.tutors[i].add_hours(hours)

    def display_tutors(self):
        """prints all Tutors in the tutor list"""
        for t in self.tutors:
            print(t)
    
    def __str__(self):
        """Displays information about NNHS Peer Tutoring, including the teacher, number of tutors, and total hours of tutoring"""
        hours = 0
        for t in self.tutors:
            hours += t.get_hours()
        string = "\nNNHS Peer Tutoring" + "\nTeacher: " + self.teacher + "\nNumber of active tutors: " + str(len(self.tutors))
        string += "\nTotal hours tutored: " + str(hours)
        return string
# main function for the Peer Tutoring App
#   Complete the implementation of this function so the app works as intended.
from peer_tutoring import *
from tutor import *

def main():
  menu = """
        1. Display all tutors
        2. Find a tutor (by subject)
        3. Find a tutor (by day)
        4. Add a tutor
        5. Remove a tutor (by name)
        6. Add tutoring hours
        7. NNHS Tutoring Stats
        0. Exit
    """
  tutor1 = Tutor("Lisa Simpson", 11, ["Band", "Mathematics", "Biology"], ["Monday", "Wednesday"])
  tutor2 = Tutor("Spongebob Squarepants", 9, ["Social Studies", "Art"], ["Wednesday"])
  tutor3 = Tutor("Bender", 12, ["Computer Science", "Mathematics", "Statistics"], ["Wednesday", "Friday"])
  tutor4 = Tutor("April O'Neil", 10, ["Social Studies", "History", "English"], ["Tuesday", "Thursday"])
  tutor5 = Tutor("Mickey Mouse", 9, ["Art", "Science"], ["Monday", "Tuesday"])
  tutor6 = Tutor("Black Panther", 10, ["Mathematics", "Science"], ["Tuesday", "Friday"])
  tutor7 = Tutor("Princess Peach", 12, ["Culinary", "History"], ["Thursday", "Friday"])
  pt = Peer_tutoring("")
    # initialize Peer_tutoring object with initial list of tutors
  nnhs_tutors = Peer_tutoring([tutor1, tutor2, tutor3, tutor4, tutor5, tutor6, tutor7])

    # run the app
  print("\nWelcome to the NNHS Peer Tutoring App!\n")
  choice = "1"
  addsub = []
  newsub = ""
  newgrade = 10
  while choice != "0":
    print(menu)
    choice = input("\nSelect an option: ")
    subjec = ""
    if choice == "1":
      nnhs_tutors.display_tutors()
    elif choice == "2":
      subjec = input("What is the name of the subject you'd like to see available tutors for? ")
      nnhs_tutors.find_tutor_subj(subjec)
    elif choice == "3":
      days = input("What is the day you'd like to see available tutors for? ")
      nnhs_tutors.find_tutor_day(days)
    elif choice == "4":
      newname = input("What is the name of the new tutor? ")
      if newgrade > 8 and newgrade < 13:
        newgrade = input("What is the grade of the new tutor (9-12) ? ")
      elif newgrade <= 8 and newgrade >= 13:
        print("Please enter a number between 9 and 12.")
      while newsub != "":
        newsub = input("Subject to add? (Leaven empty if done) ")
        addsub.append(newsub)
      nnhs_tutors.add_tutor(addsub)
    elif choice == "5":
      removed = input("Which tutor would you like to remove? ")
      nnhs_tutors.remove_tutor(removed)
    elif choice == "6":
      addername = input("Name of tutor to add hours to? ")
      adderhour = input("Numbers of hours to add? ")
      nnhs_tutors.add_hours(addername, adderhour)
    elif choice == "7":
      print("Tutoring status:")
      print("Teacher: " )
      print("Numbers of active tutors: ")
      print("Total hours tutored: ")
    elif choice == "0":
      print("\nThanks for using the NNHS Peer Tutoring App!")

if __name__ == "__main__":
    main()

如果我在程序運行時輸入 1,它應該會顯示當前所有學生的信息。

如果我輸入 2,它應該問我你想搜索的主題是什么。 一旦我進入它

如果我輸入 3,它應該按天搜索(從周一到周五)並列出匹配日期的學生。

如果我輸入 4,它應該通過輸入他們的姓名、年級和學科來添加導師。

如果我輸入 5,它應該通過輸入姓名從輔導列表中刪除一個輔導老師。

如果我輸入 6,它應該問我要向誰添加輔導時間以及總共要添加多少小時。

如果我輸入 7,它應該會顯示教師姓名、活躍導師人數和總輔導時數。

選項 1 沒有打印出任何內容(這是我要解決的最基本的問題),除此之外,選項 2 和選項 3 也有同樣的問題。 4、5、6無法完全測試出來,因為選項1無法生效。 所以,據我所知,現在只有 7 個在工作。 (布魯赫)

此外,選項 4 中存在一個問題:如果您沒有輸入 9-12 之間的數字來定義新導師的等級,則不會彈出通知您沒有按照說明操作的消息。

有人願意給我一些指示嗎?

編輯:問題中未包含選項 1 的錯誤。 我真誠的道歉。

您的問題不包括回溯,但是當我運行代碼時,我得到了:

Select an option: 1
Traceback (most recent call last):
  File "test.py", line 167, in <module>
    main()
  File "test.py", line 134, in main
    nnhs_tutors.display_tutors()
  File "test.py", line 90, in display_tutors
    print(t)
  File "test.py", line 31, in __str__
    string += "\nGrade:", self.grade
TypeError: can only concatenate str (not "tuple") to str

這將我們指向您的__str__函數之一:

  def __str__(self):
    string = ""
    string += self.name
    string += "\nGrade:", self.grade
    string += "\nSubjects:", self.subjects
    string += "\nDays:", self.days
    string += "\nHours:", self.hours
    return string

正如錯誤所說,您不能將元組連接(添加)到 str —— 像"\nGrade:", self.grade是一個元組(由strint組成)。

我建議做類似的事情:

  def __str__(self):
    return f"""{self.name}
Grade: {self.grade}
Subjects: {self.subjects}
Days: {self.days}
Hours: {self.hours}"""

或者如果你想變得花哨:

  def __str__(self):
    return "\n".join(f"{a.title()}: {v}" for a, v in self.__dict__.items())

這至少修復了您的“選項 1”錯誤。 對於其他錯誤,請確保您正在查看代碼引發的異常,然后查看代碼以及錯誤消息以找出您的代碼產生該錯誤的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM