我目前正在开发一个处理用户输入的程序,我遇到了一个需要在一个input()
下多次返回的情况,起初我不知道当我得到什么时我在做什么一个值错误,直到我查找如何操作,它向我展示并使用input().split()
class userinput():
def __init__(self, name,lista,listb,listc,listd):
self.name=""
self.lista=lista
self.listb=listb
self.listc=listc
self.listd=listd
def set_lists(self):
print("Do you want to create lists")
decision = input()
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 first 2 lists to be")
self.lista,self.listb = input().split(",")
print("finally, the name for your third")
self.listc = input()
print("you have created 3 lists with the names," +self.lista,self.listb,self.listc)
elif decision2 == 4:
print("what name would you like your first 2 lists to be")
self.lista,self.listb = input().split(",")
print("finally, for your last 2 lists")
self.listc,self.listd=input().split(",")
print("you have created three lists with the names of," +self.lista,self.listb,self.listc,self.listd)
else:
print("quitting")
return
else:
print("quitting")
我的问题:似乎没有必要为4个输入使用2 input().split()
,无论如何要清理它吗?
答案 0 :(得分:2)
你有几个问题,这里有很多重复。一种方法有一些改进:
decision = input("Do you want to create lists?")
if decision.lower() in ("y", "yes"):
list_count = int(input("How many lists?"))
names = input("Enter list names (separated by commas):").split(",")
if len(names) != list_count:
raise ValueError("Expected {0} names, got {1}".format(list_count,
len(names)))
self.lists = {name: [] for name in names} # dictionary of empty lists
print("Created {0} lists.".format(list_count))
(请注意,Python 2.x使用raw_input
)。
split
创建一个包含字符串可以拆分的项目列表,除非受第二个可选参数maxsplit
约束:
"a,b,c".split(",") == ["a", "b", "c"]
"a,b,c".split(",", 1) == ["a", "b,c"]
答案 1 :(得分:1)
我认为这段代码会更清晰。
print("Do you want to create lists")
decision = input()
if decision == "yes":
print("how many lists would you like to create?(up to 4)")
decision2= int(input())
print("What would you like the lists named?")
listnames = input()
lists = listnames.split()
print("you have created %i lists with the following names: %s"%(decision2, ",".join(lists)))
else:
print("quitting")
答案 2 :(得分:0)
我只是这样做:
def set_lists(self):
print("Do you want to create lists")
decision = input()
if decision.lower().startswith("y"):
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.lists = [input()]
print("you have created 1 list, with the name:"+ self.lists[0])
elif 2 <= decision2 <= 4:
print("Enter your lists' names, seperated by commas")
self.lists = input().split(",")
if len(self.lists) != decision2:
print("You didn't enter the right amount of names!")
else:
print("You have created {} lists with the names {}".format(decision2, ", ".join(map(str, self.lists)))
else:
print("quitting")
else:
print("quitting")
然后执行self.lists[0]
,self.lists[1]
等,而不是self.lista
,self.listb
。
答案 3 :(得分:0)
a, b, c, d = raw_input("what name would you like your 4 lists to be\n").split(',')
split
可以将字符串拆分为两个以上的部分。