简体   繁体   English

如何将一个值放入另一个列表?

[英]How can I put one value in another list?

My program needs to accept inputs from the user, but the input is like Ted,24 , so every input will be separated by comma and I know putting them in a list will be a great idea but I also need to get the average age. 我的程序需要接受用户的输入,但是输入就像Ted,24 ,因此每个输入都将用逗号分隔,我知道将它们放在列表中将是一个不错的主意,但我还需要获取平均年龄。 Here's my code so far: 到目前为止,这是我的代码:

namesAge = []
count = 0
ave = 0
total = 0

while True:
    mess=input("Enter name and age: ")
    if mess == "":
        print(namesAge)
        print ("We have ",count," names")
        print ("Total age is",total)
        break
    if "," in mess:
        namesAge.append(mess)
        count = count + 1
    else:
        print("invalid input")
namesAge = []

while True:
    mess=input("Enter name and age: ")
    if mess == "":
        print(namesAge)
        print ("We have {} names".format(len(namesAge)))
        print ("average age is {}".format(sum(namesAge)/len(namesAge))))
        break
    elif "," in mess:
        name, age = mess.split(',')
        namesAge.append(int(age))
    else:
        print("invalid input")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表元素之前和之后放置一个空格? - How I can put one space before and after list elements? 如何将数字从一个列表放入另一个列表? - How to put numbers from one list to another? 如何比较一本字典的价值和另一本字典的价值? - How can I compare value of one dictionary to another and get that value? 如何使用Python从单词列表中删除不需要的字符并将其清除到另一个列表中? - How can I remove unwanted characters from a words list and put them cleared in another list using Python? 如何验证一个列表是否是另一个列表的子集? - How can I verify if one list is a subset of another? 如何检查一个列表是否是另一个列表的子集? - How can I check if one list is a subset of the another? 如何使用Python defaultdict将一个值除以另一个? - How can I use a Python defaultdict to divide one value by another? 如何将一个函数中的值传输到另一个函数? - How can I transfer the value in one function to another function? 我如何使用一个变量的值在python中调用另一个变量? - How can i use the value of one variable to call another in python? 如果我不使用集合,如何检查一个列表中的值是否在带有if语句的单行代码的另一个列表中? - How to check if a value in one list is in another list with a one-liner for an if statement in Python if I'm not using sets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM