Python 输入中的数据不被接受为参数
当我直接在函数中输入数据时,我得到了正确的输出,但是当我使用用户的输入来填充列表时,什么也没有发生。我没有收到任何错误,也没有任何输出 输入的数据应进入列表,输入的索引应从列表中删除Python 输入中的数据不被接受为参数,python,Python,当我直接在函数中输入数据时,我得到了正确的输出,但是当我使用用户的输入来填充列表时,什么也没有发生。我没有收到任何错误,也没有任何输出 输入的数据应进入列表,输入的索引应从列表中删除 #!/usr/bin/env python3 #class definitions class record: def __init__(self,telephone,lastname,firstname): self.telephone = telephone self
#!/usr/bin/env python3
#class definitions
class record:
def __init__(self,telephone,lastname,firstname):
self.telephone = telephone
self.lastname = lastname
self.firstname = firstname
def __str__(self):
return f"Last name: {self.lastname}, First Name: {self.firstname}, Telephone: {self.telephone}"
class PhoneBook:
def __init__(self):
self.phonebook = []
def addrecord(self, record):
self.phonebook.append(record)
return self.phonebook.index(record)
def deleterecord(self, i):
self.phonebook.pop(i-1)
def printphonebook(self):
x = 1
for entry in self.phonebook:
print(x,'. ',entry,sep='')
x = x + 1
#Main
select = None
while select != 'exit':
ph = PhoneBook()
ph.addrecord(record(515,'fin','matt'))
ph.addrecord(record(657,'fisher','bill'))
select = input('Main Menu \n1. show phonebook \n2. add record \n3. remove record\nor "exit" to exit program\n')
test = False
while test == False:
if select == '1':
ph.printphonebook()
test = True
elif select == '2':
x = int(input('Enter telephone number.\n'))
y = str(input('Enter last name.\n'))
z = str(input('Enter first name.\n'))
ph.addrecord(record(x,y,z))
test = True
elif select == '3':
i = int(input('Enter the record number youd like to delete.\n'))
ph.deleterecord(i)
test = True
elif select == 'exit':
break
else:
print('Invalid selection. Please try again.')
test = True
所需的输出是获取数据,以便根据我的x、y和z输入正确输入和退出列表,并根据我的输入取出列表的指定索引。每次运行第一个while循环时,您都会清除并创建一个新的电话簿对象。 我是新手,没有足够的技能在短时间内解决所有问题。 你看不到你的新条目,因为它们每次都会被删除
尝试使用一个while循环和一个switch语句。您能澄清您期望发生的事情和实际发生的事情吗?除了输入,您没有在任何地方打印任何内容,因此这不会显示任何内容。您可以在代码执行的各个点打印ph.phonebook并检查output@DeveshKumarSingh是,当我打印列表时,除了我在代码注释中指定的内容外,没有添加任何新内容。请注意,我的功能正在工作;输入是问题所在。@carcigenitate per rules of stack overflow我没有添加任何代码,因为我觉得这会影响手头的问题。我在解释和代码注释中指定了哪些部分给出了输出,哪些部分没有。@MatthewFinateri MCVEs很重要,但只有当它们准确地表示您的问题时,它们才有效。如果您希望打印,我们需要查看您希望从何处打印内容。python中没有switch语句: