python中的第一步:如何组合两个列表
所以我刚开始学习Python的一些基础知识。因为我是一个非常实际的人,所以我喜欢用Python自动化枯燥的东西 没有,这里有一章介绍python中的列表及其优点。 为了实用,应该编写一个代码,要求用户输入猫名,然后将猫名添加到列表中。如果不再添加猫名,则应显示所有猫名 到现在为止,这是很公平的。所以我想我应该尝试一下,再向前迈一小步,通过增加猫的年龄来扩展功能。期望的结果是要求用户输入姓名,然后输入年龄,再次输入姓名和年龄,依此类推。如果用户没有再次输入名字,它应该列出猫的年龄 我创建了第二个列表,也创建了第二个输入,一切都正常,但我不知道如何组合这两个列表,或者更确切地说是值 它只是先给我两个名字,然后给我两个年龄 有人愿意帮我解决这个初学者的问题吗 提前谢谢python中的第一步:如何组合两个列表,python,Python,所以我刚开始学习Python的一些基础知识。因为我是一个非常实际的人,所以我喜欢用Python自动化枯燥的东西 没有,这里有一章介绍python中的列表及其优点。 为了实用,应该编写一个代码,要求用户输入猫名,然后将猫名添加到列表中。如果不再添加猫名,则应显示所有猫名 到现在为止,这是很公平的。所以我想我应该尝试一下,再向前迈一小步,通过增加猫的年龄来扩展功能。期望的结果是要求用户输入姓名,然后输入年龄,再次输入姓名和年龄,依此类推。如果用户没有再次输入名字,它应该列出猫的年龄 我创建了第二个列
catNames = []
catAges = []
while True:
print("Enter the name of Cat " + str(len(catNames) + 1) + "(Or enter
nothing to stop.)")
name = input()
while name !="":
print("Enter the age of cat ")
age = input()
break
if name == "":
print("The cat names and ages are: ")
for name in catNames:
print(" " + name)
for age in catAges:
print(" " + age)
break
catNames = catNames + [name]
catAges = catAges + [age]
我想你在寻找: 输出: 一般来说,你会用它来完成这类任务 但是,如果要使用列表解决问题,可以这样实现:
catNames = []
catAges = []
while True:
print("Enter the name of Cat " + str(len(catNames) + 1) + "(Or enter nothing to stop.)")
name = input()
while name !="":
print("Enter the age of cat ")
age = input()
break
if name == "":
print("The cat names and ages are: ")
for i in range(len(catNames)):
print("Cat number",i, "has the name", catNames[i], "and is", catAges[i], "years old")
break
catNames = catNames + [name]
catAges = catAges + [age]
如果我理解正确的话,你想把年龄和名字一起打印出来吗? 如果是这样,你可以这样做:
catNames = []
catAges = []
while True:
name = input("Enter the name of Cat {} (Or enter nothing to stop): ".format(str(len(catNames) + 1)))
while name != "":
age = input("Enter the age of {}: ".format(name)) # Takes inputted name and adds it to the print function.
catNames.append(name) # Adds the newest name the end of the catNames list.
catAges.append(age) # Adds the newest age the end of the catNames list.
break
if name == "":
print("\nThe cat names and ages are: ")
for n in range(len(catNames)):
print("\nName: {}\nAge: {}".format(catNames[n], catAges[n]))
break
结果输出:
Enter the name of Cat 1 (Or enter nothing to stop): Cat1
Enter the age of Cat1: 5
Enter the name of Cat 2 (Or enter nothing to stop): Cat2
Enter the age of Cat2: 7
Enter the name of Cat 3 (Or enter nothing to stop): # I hit enter here so it skips.
The cat names and ages are:
Name: Cat1
Age: 5
Name: Cat2
Age: 7
如果你对我所做的有任何疑问,请尽管问 对不起,你到底想做什么?你得到的输出是什么,它与你想要的输出有何不同;博士,请试着直接切入点。事实上,AronC发布的解决方案和结果完美地展示了我想要的结果!!太好了,谢谢。基本上,我是在寻找catNamesPerfect中for名称的替代品,这正是我所寻找的!
catNames = []
catAges = []
while True:
name = input("Enter the name of Cat {} (Or enter nothing to stop): ".format(str(len(catNames) + 1)))
while name != "":
age = input("Enter the age of {}: ".format(name)) # Takes inputted name and adds it to the print function.
catNames.append(name) # Adds the newest name the end of the catNames list.
catAges.append(age) # Adds the newest age the end of the catNames list.
break
if name == "":
print("\nThe cat names and ages are: ")
for n in range(len(catNames)):
print("\nName: {}\nAge: {}".format(catNames[n], catAges[n]))
break
Enter the name of Cat 1 (Or enter nothing to stop): Cat1
Enter the age of Cat1: 5
Enter the name of Cat 2 (Or enter nothing to stop): Cat2
Enter the age of Cat2: 7
Enter the name of Cat 3 (Or enter nothing to stop): # I hit enter here so it skips.
The cat names and ages are:
Name: Cat1
Age: 5
Name: Cat2
Age: 7