尝试调试同伴辅导模拟程序的逻辑错误



大家好,所以基本上,我试图完成一项编程任务,但似乎需要一些备份。

这是我现在拥有的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个在工作。(Bruh(

此外,选项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";错误对于其他错误,请确保您正在查看代码引发的异常,然后查看代码和错误消息,以找出代码产生该错误的原因。

最新更新