最初,当我编写函数时,我没有在set_list
函数中包含print(UserInput.users)
,但是,我认为列表向用户显示他们能够创建哪些列表会很有帮助。删除它后,该功能无法正常工作:
class UserInput:
users=[]
def __init__(self, name,lista,listb,listc,listd):
self.name=""
self.lista=lista
self.listb=listb
self.listc=listc
self.listd=listd
@classmethod
def create_new_users(cls):
print("how many users do you want to create")
x=int(input())
for _ in range(x):
print("assign the users names")
name = input()
if name == '' or name.lower() == 'none':
raise RuntimeError("name cannot be None or empty")
user=cls(name,"","","","")
cls.users.append(name)
return(name)
@classmethod
def show_users(cls):
print(UserInput.users)
def set_lists():
print("Do you want to create lists")
decision = input()
print( "select the user you intend on adding lists for")
#why does the function require the code print(UserInput.users) on this line to function properly?
choice = input()
for elem in UserInput.users:
if choice == elem:
if decision == "yes":
print("how many lists would you like to create?(up to 4)")
decision2= int(input())
if decision2 == 1:
print("what would you like the list to be named?")
self.lista=input()
print("you have created 1 list, with the name:"+ self.lista)
elif decision2 == 2:
print("what would you like your lists to be?")
self.lista,self.listb=input().split(",")
print("You have created 2 lists with the names," + self.lista ,self.listb)
elif decision2 == 3:
print("what name would you like your lists to be?")
self.lista,self.listb,self.listc = input().split(",")
print("you have created 3 lists with the names," +self.lista,self.listb,self.listc)
elif decision2 == 4:
print("what name would you lists to be?")
self.lista,self.listb,self.listc,self.listd = input().split(",")
print("you have created 4 lists with the names of," +self.lista,self.listb,self.listc,self.listd)
else:
print("quitting")
return
else:
print("not in users list")
break
我的问题:当我从中选择一个名称时,print(UserInput.users)
包含在函数中时,它将识别列表,但如果未包含,它会跳到底部的其他名称:为什么?
搜索代码会发现至少一个混合制表符和空格的实例。这可能是问题所在,但我对此表示怀疑。相反,我怀疑这是一个逻辑错误,与您删除的行无关。
for elem in UserInput.users:
if choice == elem:
stuuuuuuuuuff...
else:
print("not in users list")
break
如果choice
不是UserInput.users
的第一个元素,这将在第一次迭代时命中else
子句并中断。我怀疑您只想在choice
根本不在列表中时才这样做。与其循环,我推荐使用 in
运算符:
if choice in UserInput.users:
stuuuuuuuuuuff...
else:
print("Not in user list.")