Python 关于input().split()与3+;项目
我目前正在开发一个处理用户输入的程序,我遇到了一种情况,在一个Python 关于input().split()与3+;项目,python,Python,我目前正在开发一个处理用户输入的程序,我遇到了一种情况,在一个input()下我需要多个返回,起初我不知道当我得到一个值错误时我在做什么,直到我查到如何做,它向我展示了如何使用input()处理两个输入。split() 我的问题:似乎没有必要对4个输入使用2input().split(),还有什么方法可以清理吗?我认为这段代码会更干净一些 print("Do you want to create lists") decision = input() if d
input()
下我需要多个返回,起初我不知道当我得到一个值错误时我在做什么,直到我查到如何做,它向我展示了如何使用input()处理两个输入。split()
我的问题:似乎没有必要对4个输入使用2
input().split()
,还有什么方法可以清理吗?我认为这段代码会更干净一些
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")
我认为这段代码会更干净一些
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")
我认为这段代码会更干净一些
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")
我认为这段代码会更干净一些
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")
我会这样做:
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")
def set_列表(自):
打印(“是否要创建列表”)
决策=输入()
如果decision.lower().startswith(“y”):
打印(“要创建多少个列表?(最多4个)”)
decision2=int(输入())
如果决策2==1:
打印(“您希望列表命名为什么”)
self.lists=[input()]
打印(“您已经创建了1个列表,名称为:“+self.lists[0]”)
elif 2我会这样做:
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")
def set_列表(自):
打印(“是否要创建列表”)
决策=输入()
如果decision.lower().startswith(“y”):
打印(“要创建多少个列表?(最多4个)”)
decision2=int(输入())
如果决策2==1:
打印(“您希望列表命名为什么”)
self.lists=[input()]
打印(“您已经创建了1个列表,名称为:“+self.lists[0]”)
elif 2我会这样做:
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")
def set_列表(自):
打印(“是否要创建列表”)
决策=输入()
如果decision.lower().startswith(“y”):
打印(“要创建多少个列表?(最多4个)”)
decision2=int(输入())
如果决策2==1:
打印(“您希望列表命名为什么”)
self.lists=[input()]
打印(“您已经创建了1个列表,名称为:“+self.lists[0]”)
elif 2我会这样做:
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")
def set_列表(自):
打印(“是否要创建列表”)
决策=输入()
如果decision.lower().startswith(“y”):
打印(“要创建多少个列表?(最多4个)”)
decision2=int(输入())
如果决策2==1:
打印(“您希望列表命名为什么”)
self.lists=[input()]
打印(“您已经创建了1个列表,名称为:“+self.lists[0]”)
elif 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))
(请注意,Python2.x使用原始输入
)
split
创建一个包含尽可能多的项目的列表,除非受第二个可选参数的约束maxslit
:
"a,b,c".split(",") == ["a", "b", "c"]
"a,b,c".split(",", 1) == ["a", "b,c"]
你有几个问题,这里有很多重复。这是一种有几处改进的方法:
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))
(请注意,Python2.x使用原始输入
)
split
创建一个包含尽可能多的项目的列表,除非受第二个可选参数的约束maxslit
:
"a,b,c".split(",") == ["a", "b", "c"]
"a,b,c".split(",", 1) == ["a", "b,c"]
你有几个问题,这里有很多重复。这是一种有几处改进的方法:
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))
(请注意,Python2.x使用原始输入
)
split
创建一个包含尽可能多的项目的列表,除非受第二个可选参数的约束maxslit
:
"a,b,c".split(",") == ["a", "b", "c"]
"a,b,c".split(",", 1) == ["a", "b,c"]
你有几个问题,这里有很多重复。这是一种有几处改进的方法:
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))
(请注意,Python2.x使用原始输入
)
split
创建一个包含尽可能多的项目的列表,除非受第二个可选参数的约束maxslit
:
"a,b,c".split(",") == ["a", "b", "c"]
"a,b,c".split(",", 1) == ["a", "b,c"]
split
可以将字符串拆分为两个以上的部分
split
可以将字符串拆分为两个以上的部分
split
可以将字符串拆分为两个以上的部分
split
可以将字符串拆分为两部分以上。您不应该执行print().split()
。事实上,我相信这会产生一个错误(除非您使用的是Python2.x,这似乎不太可能,因为print
被用作一个函数)。@sweeneyrod我指的是input().split(),感谢您的更正,我指的是这一行-print(“最后,对于您的最后两个列表”).split(,)
@Izkata仅在Python 2中使用。x@Izkata它很可能是Python3,因为打印用作函数和input
而不是raw\u input
。您不应该执行print().split()
。事实上,我相信这会产生一个错误(除非您使用的是Python2.x,这似乎不太可能,因为print
被用作一个函数)。@sweeneyrod我指的是input().split(),感谢您的更正,我指的是这一行-print(“最后,对于您的最后两个列表”).split(,)
@Izkata仅在Python 2中使用。x@Izkata它很可能是Python3,因为打印用作函数和input
而不是raw\u input
。您不应该执行print().split()
。事实上,我相信这会产生一个错误(除非您使用的是Python2.x,这似乎不太可能,因为print
被用作一个函数)。@sweeneyrod我指的是input().split(),感谢您的更正,我指的是这一行-print(“最后,对于您的最后两个列表”).split(,)
@Izkata仅在Python 2中使用。x@Izkata它很可能是Python3,因为美国印刷