错误,列表对象不可调用

时间:2017-06-13 17:57:48

标签: python python-3.5 pythonanywhere

这是我的代码:

firstname = ["Mark", "Rohan", "Matt" , "Anthony" , "Teddy" , "Kyle" , "Bob"]
lastname = ["Wightman", "Red", "Silver" , "Blue" , "Green" , "Orange" , "Purple"]
age = ["15",'5', '16', '21years', '17.6', '19', '-18', '917']

input_firstname = input('What is first name?: ')
input_lastname = input('What about your last name?: ')
input_age = input('What is your age?: ')

firstname.append(input_firstname)
lastname.append(input_lastname)
age.append(input_age)

index = 0
valid = True
for original in age:
    if len(original) < 2:
        valid = False
    elif original.isdigit() == False:
        valid = False
    elif (len(original) > 2) and original[2].isdigit():
        valid = False
    elif len(original) > 2 and original[2] == ".":
        neworiginal = original[0:2]
        original = neworiginal
    elif valid == True:
        print ("%s, %s, %s") %(firstname(original), lastname(original), age(original))
    index = index +1

此程序应该打印第一个名称列,然后是姓氏列,然后是年龄列,例如: 马克,怀特曼,15岁 Rohan,Red,5

等等。但是,当代码运行时,会出现错误,说明&#34;打印(&#34;%s,%s,%s&#34;)%(名字(原始),姓氏(原始), age(original)&#34;&#34; list&#34;对象不可调用。此项目的目标是使用list,for循环和条件来清理age数据并将其打印成3列。

2 个答案:

答案 0 :(得分:0)

如果要获取列表元素

,请使用方括号而不是圆形

我的意思是名字[原始]而不是名字(原始)

答案 1 :(得分:0)

正如其他答案所述,您需要使用括号[],但您还需要将original转换为integer,以便firstname[int(original)]等等:

print ("%s, %s, %s") % (firstname[int(original)], lastname[int(original)], age[int(original)])

编辑:

for i in range(len(firstname)):
    print ("%s, %s, %s") % (firstname[i], lastname[i], age[i])