python - 将新节点附加到链表的末尾
问题描述
我正在尝试创建约会的链接列表,但是当我运行测试代码时,它给了我一个类型错误,说我缺少 3 个位置参数。我想解决方案很简单,但我一直在修补它一段时间,无法弄清楚。
from datetime import datetime
class VaccList:
class Appointment:
def __init__(self, name, age, city, date):
assert type(name) is str, 'name variable must be a string'
assert type(age) is int, 'age variable must be a integer'
assert type(city) is str, 'city variable must be a string'
assert type(date) is datetime, 'date variable must be a datetime object'
assert name != None, 'name variable cannot be empty'
assert age >= 18 and age <= 100, 'age must be between 18 and 100'
# ADD 6 asserts. 4 for the types and name cannot be empty,
# and age must be between 18 and 100
self.name = name
self.age = age
self.city = city
self.date = date
self.confirmed = False
self.next = None
def __str__(self):
s = "Appointment for " + self.name + " on " + str(self.date) + " age:" + str(
self.age) + " city:" + self.city
if self.confirmed:
s += " (confirmed)"
else:
s += " (unconfirmed)"
return s
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
return self.head is None
def print(self):
'''
Print all the appointments, one per line. Print a blank line after the last one.
If the list is empty, print a line saying the Appointment List is empty.
'''
pass
def append(self, newAppt):
''' Given a pointer to an Appointment object, tack it onto the end.
If the list was empty, then make sure to set both pointers!
This is only used for appointments that are canceled or completed,
since we want the active list to remain sorted by date.
'''
assert type(newAppt) is VaccList.Appointment, "append() requires a pointer to an Appointment object."
# Note, no loop is needed!
newnode = VaccList.Appointment(newAppt)
if self.head is None:
self.head = newnode
else:
newnode.next = self.head
self.head = newnode
if __name__ == "__main__":
active = VaccList()
appt = VaccList.Appointment("Henry", 72, "Buffalo", datetime(2021, 5, 1, 12, 0, 0))
active.append(appt)
以下是我一直遇到的以下错误:
Traceback (most recent call last):
File "/Users/nicholas/Downloads/SHELL/vacclist.py", line 152, in <module>
active.append(appt)
File "/Users/nicholas/Downloads/SHELL/vacclist.py", line 54, in append
newnode = VaccList.Appointment(newAppt)
TypeError: __init__() missing 3 required positional arguments: 'age', 'city', and 'date'
由于我正处于学习 python 的初始阶段,因此对此的任何帮助都非常感谢。
解决方案
推荐阅读
- javascript - 从 javascript 对象创建数组
- powershell - PowerShell解析每一行并在冒号后提取值
- python - 如何打开流而不是将整个文件加载到pyhton lambda中的内存中
- python - 将 Pandas Dataframe 行插入列表,每个值都有标题
- json - 如何以角度修复不可迭代的对象?
- visual-studio - 如何使 Visual Studio 构建因 ReSharper 错误而失败
- vue.js - 如何提高 VueJS Vuetify 性能
- javascript - 仅单击插入符号后如何显示下拉列表?
- java - IntelliJ / Gradle - 复合构建项目 - 如何从不同的模块导入?
- html - 如何在从 URL 将数据(pdf)加载到 HTML 对象元素中时显示微调器或进度