集合方法set错误求助
小白求组,问题代码如下import easygui as p
class Variable():
def getVariable(self):
while 1:
list_object = []
set_object = {}
object_range = 0
choice_variables = ["增加实例对象","删除实例对象"]
user_choice1 = p.choicebox("请选择您的操作","输入提示",choice_variables)
if user_choice1 == "增加实例对象":
Entity_object = p.enterbox("请输入实例对象","增加提示")
list_object = list_object.extend(Entity_object)
set_object = set(list_object)
object_range = len(set_object)
Entity_object = Variable()
print("此时类Variable中的实例对象一共有%d个" % object_range)
print("此时类中所有的实例对象为",set_object)
if user_choice1 == "删除实例对象":
Entity_object = p.enterbox("请输入实例对象","删除提示")
list_object = list_object.remove(Entity_object)
set_object = set(list_object)
object_range = len(set_object)
print("此时类Variable中的实例对象一共有%d个" % object_range)
print("此时类中所有的实例对象为",set_object)
已尝试过各种方法,结果一运行pyhton就反馈set_object集合是空的,list_object列表也是空的,明明都没有问题呀。。请高人指点 给你修改了下,主要的问题,列表的append()(这边要用append,不是extend,试一下就知道区别了)和remove()函数没有返回值,所以用变量接收后就是None
import easygui as p
class Variable():
def getVariable(self):
list_object = []
while True:
choice_variables = ["增加实例对象", "删除实例对象"]
user_choice1 = p.choicebox("请选择您的操作", "输入提示", choice_variables)
if user_choice1 == "增加实例对象":
Entity_object = p.enterbox("请输入实例对象", "增加提示")
if Entity_object != '':
list_object.append(Entity_object)
print(list_object)
set_object = set(list_object)
object_range = len(set_object)
print("此时类Variable中的实例对象一共有%d个" % object_range)
print("此时类中所有的实例对象为", set_object)
else:
print('未输入内容')
if user_choice1 == "删除实例对象" and user_choice1 != '':
Entity_object = p.enterbox("请输入实例对象", "删除提示")
if Entity_object != '' and Entity_object in list_object:
list_object.remove(Entity_object)
print(list_object)
set_object = set(list_object)
object_range = len(set_object)
print("此时类Variable中的实例对象一共有%d个" % object_range)
print("此时类中所有的实例对象为", set_object)
else:
print('未输入内容或输入的内容不在列表内')
a = Variable()
a.getVariable() 疾风怪盗 发表于 2020-9-25 00:47
给你修改了下,主要的问题,列表的append()(这边要用append,不是extend,试一下就知道区别了)和remove() ...
感谢指点,这么基础的问题一敲起来就忽略了。。。
页:
[1]