TypeError:' int'对象不可订阅

时间:2016-06-14 00:52:15

标签: list if-statement while-loop int typeerror

所以基本上我必须告诉用户在电话商店工作的员工是否已获得奖金。如果员工售出4部或更多电话,他们会获得奖金。

问题是我一直收到这个错误。

VS="/c/Users/name/Documents/Visual\ Studio\ 2015/Projects"

VS="/c/Users/name/Documents/Visual Studio 2015/Projects"

这是我的代码 -

    if  list[member_number]>=4:
TypeError: 'int' object is not subscriptable

2 个答案:

答案 0 :(得分:0)

您有两个list个对象。一个是数组,另一个是for语句中的对象,这里是:

for list in range(0,How_Many_Members,1):

您正在使用重复项,但这并不好,导致您的程序吐出错误。它使用list而不是数组list。由于for循环中的list是一个整数对象,因此抛出错误。你基本上试图在数组上使用下标,但是它将它误认为是for循环中的整数,因为它是不明确的。请尝试以下方法:

How_Many_Members = int(input("Enter the number of staff members:"))

list = []
member = 1

while len(list) != How_Many_Members:
    print("how many phones did the member", member, "sold?", end=" ")

    Sales = int(input(""))
    list.append(Sales)
    member += 1

member_number = 0

for _ in range(0, How_Many_Members, 1):
    if list[member_number] >= 4:
        print("member", member_number + 1, "has earned a bonus")
        member_number += 1
    else:
        print("member", member_number + 1, "has not earned a bonus")
        member_number += 1

还有别的,你在几个地方拼错了member_number,我为你修好了。我还简短了一些陈述,让它打印member 1而不是member 0

答案 1 :(得分:0)

你的问题在这里:

for list in range(0,How_Many_Members,1):
    if  list[member_number]>=4:
        print("member",memeber_number,"has earned a bonus")
        member_number=member_number+1
    else:
        print("member",memeber_number,"has not earned a bonus")
        member_number=member_number+1

你在说范围(0,How_Many_Members)中的列表,所以list是从0到How_Many_Members-1的整数值。所以它不再是一个列表,你不能列出[member_number]。