简体   繁体   English

如何解决此错误文件“test.py”,第 5 行,在<module> for _ in range(b[0]): TypeError: 'int' object 不可订阅</module>

[英]how to resolve this error File "test.py", line 5, in <module> for _ in range(b[0]): TypeError: 'int' object is not subscriptable

I am trying to solve this problem:我正在尝试解决这个问题:

Covid-19 is spreading fast! Covid-19 正在迅速传播! There are N cities, numbered from 0 to N−1 , arranged in a circular manner.N个城市,编号从0N−1 ,以圆形方式排列。 City 0 is connected to city 1 , 1 to 2 , …, and city N−1 to city 0 .城市0连接到城市1连接到2 ,...,城市N−1 1到城市0

The virus is currently at city X .该病毒目前在X市。 Each day, it jumps from its current city, to the city K to its right, ie, from city X to the city (X+K)%N .每天,它从当前城市跳到它右边的城市K ,即从城市X跳到城市(X+K)%N As the virus jumps, the cities in between don't get infected.随着病毒的传播,中间的城市不会被感染。 Cities once infected stay infected.曾经被感染的城市会一直被感染。 You live in city Y .你住在Y市。 Find if it will reach your city eventually.看看它最终是否会到达你的城市。 If it will, print YES, else print NO.如果会,则打印 YES,否则打印 NO。

The first line of the input consists of an integer T , the number of test cases.输入的第一行包含一个 integer T ,即测试用例的数量。

The first and only line of each test case contains four space-separated integers - N , K , X and Y , denoting the number of cities, the size of jumps, Covid's current city, and the city that you live in, respectively.每个测试用例的第一行也是唯一一行包含四个以空格分隔的整数 - NKXY ,分别表示城市数量、跳跃的大小、Covid 当前城市和您居住的城市。

This is the code I tried for it:这是我试过的代码:

print("1- first write the number of times the program should run \n 2- write the of number of cities \n 3- size of jumps \n 4- virus current city \n 5- city you live in")

for _ in range(int(input())):
    a=[int(x) for x in input(). split()]
    for b in a:
        for _ in range(b[0]):
            b[2]=(b[2]+b[1])%b[0]
            if b[2]==b[3]:
                print("yes")
            else:
                print("NO")

Now I write this code as the first line will tell me how many times the code will run.现在我写这段代码,因为第一行会告诉我代码将运行多少次。 Second line helps me to get the four values which will be different as to how many times the code will run that is a[0],a[1]..a[3] .第二行帮助我获得四个值,这四个值对于代码将运行的次数不同,即a[0],a[1]..a[3] and the third line will ittrate the first values of a into b .第三行会将 a 的第a值转换为b

But I am getting an error:但我收到一个错误:

Traceback (most recent call last):
  File "covid.py", line 6, in <module>
    for _ in range(b[0]):
TypeError: 'int' object is not subscriptable

Example input:输入示例:

2
6 2 5 3
12 3 4 2

Expected output:预计 output:

YES
NO

Code is hard to read but i'll try my best to solve this: a is an integer, and integers can't be subscripted(basically accessed like an array eg. list[0], string[0:len(string)], tupl[0]) .代码很难读,但我会尽力解决这个问题:a 是 integer,整数不能下标(基本上像数组一样访问,例如list[0], string[0:len(string)], tupl[0])

In your code, you have a defined as a list with values of integers.在您的代码中,您将a定义为具有整数值的列表。 You are iterating through the list and trying to subscript the integer.您正在遍历列表并尝试下标 integer。

Let's take an example of the items you may enter as inputs: 10, 20, 30, ABCD, XYZ让我们以您可以输入的项目为例:10、20、30、ABCD、XYZ

You are expecting the below line of code to split the data into multiple elements.您期望下面的代码行将数据拆分为多个元素。 As you can see from the input, you have integers and strings.从输入中可以看出,您有整数和字符串。 Your code will fail when it tries to process the list comprehension for a当您的代码尝试处理列表推导a时,它会失败

a=[int(x) for x in input().split()]

Let's assume you entered all numeric values and also integers, say:假设您输入了所有数值和整数,例如:

10 20 30 40 50

Now a will have [10, 20, 30, 40, 50]现在a将有 [10, 20, 30, 40, 50]

Look at the next few lines of code:看下面几行代码:

for b in a:
    for _ in range(b[0]):
        b[2]=(b[2]+b[1])%b[0]
        if b[2]==b[3]:

What do you think is happening here?你认为这里发生了什么? For the first iteration, b will have a value of 10. It is not a string and not a list.对于第一次迭代,b 的值为 10。它不是字符串也不是列表。 So the next line for _ in range(b[0]): will fail.因此下一行for _ in range(b[0]):将失败。 Similarly, any reference of b[i] where i is an index of b will fail.类似地,任何对b[i]的引用都将失败,其中ib的索引。

I have marked the lines that may be in error.我已经标记了可能出错的行。 Please review them.请检查它们。

a=[int(x) for x in input().split()]
for b in a:
    for _ in range(b[0]): #are you sure b is a list? this will fail
        b[2]=(b[2]+b[1])%b[0]. #you cannot do this as b is not a list
        if b[2]==b[3]: #same here, this will fail
            print("yes")
        else:
            print("NO")

Instead you can just use a and do everything you are trying to do within the loop for b in a: Instead of the entire section of the for loop, just add a line相反,您可以只使用a并在循环for b in a:除了 for 循环的整个部分,只需添加一行

b = (a[2]+a[1]) % a[0]

if b == a[3]:
    print("yes")
else:
    print("NO")

I am trying to solve this problem:我正在尝试解决这个问题:

Covid-19 is spreading fast! Covid-19正在迅速传播! There are N cities, numbered from 0 to N−1 , arranged in a circular manner.循环排列N个城市,编号从0N−1 City 0 is connected to city 1 , 1 to 2 , …, and city N−1 to city 0 .0被连接到城市112 ,...,和城市N−1至城市0

The virus is currently at city X .该病毒目前在X市。 Each day, it jumps from its current city, to the city K to its right, ie, from city X to the city (X+K)%N .每天,它从当前城市跳转到右侧的城市K ,即从城市X跳转到城市(X+K)%N As the virus jumps, the cities in between don't get infected.随着病毒的跳跃,中间的城市不会被感染。 Cities once infected stay infected.曾经被感染的城市会继续受到感染。 You live in city Y .你住在Y市。 Find if it will reach your city eventually.查找它最终是否会到达您的城市。 If it will, print YES, else print NO.如果可以,请打印“是”,否则请打印“否”。

The first line of the input consists of an integer T , the number of test cases.输入的第一行包含一个整数T ,即测试用例的数量。

The first and only line of each test case contains four space-separated integers - N , K , X and Y , denoting the number of cities, the size of jumps, Covid's current city, and the city that you live in, respectively.每个测试用例的第一行和唯一行包含四个以空格分隔的整数NKXY ,分别表示城市的数目,跳转的大小,Covid的当前城市和您所居住的城市。

This is the code I tried for it:这是我尝试过的代码:

print("1- first write the number of times the program should run \n 2- write the of number of cities \n 3- size of jumps \n 4- virus current city \n 5- city you live in")

for _ in range(int(input())):
    a=[int(x) for x in input(). split()]
    for b in a:
        for _ in range(b[0]):
            b[2]=(b[2]+b[1])%b[0]
            if b[2]==b[3]:
                print("yes")
            else:
                print("NO")

Now I write this code as the first line will tell me how many times the code will run.现在,我编写此代码,因为第一行将告诉我代码将运行多少次。 Second line helps me to get the four values which will be different as to how many times the code will run that is a[0],a[1]..a[3] .第二行帮助我获得四个值,它们在代码将运行多少次方面是不同的,即a[0],a[1]..a[3] and the third line will ittrate the first values of a into b .和第三线将ittrate的第一值ab

But I am getting an error:但我收到一个错误:

Traceback (most recent call last):
  File "covid.py", line 6, in <module>
    for _ in range(b[0]):
TypeError: 'int' object is not subscriptable

Example input:输入示例:

2
6 2 5 3
12 3 4 2

Expected output:预期产量:

YES
NO

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM