在Python中我需要一些基本的东西

时间:2016-06-23 06:00:00

标签: python list

name = " "
Age = " "
Gender = " "
people = []
ages = []
while  name != "":
### This is the Raw data input portion and the ablity to stop the program and exit
    name = input("Enter a name or type done:")
    if name == 'done' : break
    Age = input("How old are they?")
    Gender = input("What is their gender M/F?")
### This is where I use .append to create the entry of the list     
    people.append(name)
    people.append(Age)
    ages.append(Age)
    people.append(Gender)
print("list of People:", people)

print(ages)

当我得到结果时,列表就像这样

list of People: ['jim', '12', 'F', 'Ann', '12', 'F']
['12', '12']

如何删除''在每个条目之间使我无法使用Mean函数从年龄列表中提取平均值

帮助!!!

1 个答案:

答案 0 :(得分:1)

问题是,input总是返回一个字符串。只需使用int将其转换为整数:

Age = int(input("How old are they?"))

但请注意,如果您不输入数字,这将抛出ValueError。如果您愿意,可以使用try / except来处理错误。您可以取消当前人员的输入并打印错误消息,然后从新人开始:

try:
    Age = int(input("How old are they?"))
except ValueError:
    print("Invalid input.")
    continue