how to fix int object is not subscriptable [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
this is my code currently:
previous = 0
count = 0
counts = []
item = str(data)
for x in range(len(item)):
if dates[x][3] != previous[3] or dates[x][4] != previous[4]:
if negative[x] != "No Negative":
counts.append(count)
count = 0
count += 1
previous = dates[x]
print(counts)
I keep getting an "'int' object is not subscriptable error. my values for dates and data are lists and I'm assuming the issue is that they aren't strings but nothing I do fixes the error.
thank you

The problem is in previous[3].
previous is int, it's not subscriptable.

Related

how can i fix the problem in for loop in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
error in for loop in python
I wanted to run this code, but this error occurs in the for section
h want to know why i cant use unequal in for
x = 18
y = 12
for n !=0:
n = x%y
print(n)
A mentioned by #Gabio, you should use a while loop like:
x = 18
y = 12
while n !=0:
n = x%y
print(n)
A for loop requires a defined range/collection of items to iterate over, whereas a while loop executes until a condition is met:
for n in range(0,10):
# do something

Why am I getting this Error message- (Type Error- 'type' not subscriptable) and how do I fix it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am practicing list and string methods. In the if statement I am trying to make a letter upper case however, I am getting type error not subscriptable. Thanks for your help!
a = "Halloween"
a_list = list(a)
print("1.", a_list)
a_list[3] = a_list[3].upper()
print("2. ",a_list)
a_list[0] = a_list[0].lower()
print("3. ", a_list)
aword = "".join(a_list)
print("3.", aword)
asentence= "Happy Monday Python class"
asent_list = asentence.split()
print(asent_list)
for i in range (len(a_list)):
if a-list[i] == "":
a_list[i + 1] = a_list[i + 1].upper()
print(a_list)
Looks like you've made a typo. a-list[i] should be a_list[i].

is there anything i can do to make this while loop stop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am trying to check if a certain list item is present in a given string?!
str = '[{()'
listt = ['()','{}', '[]']
replace = True
while replace:
for i in listt:
if i in str:
print('y')
replace == False
Try doing this:
str = '[{()'
listt = ['()','{}', '[]']
replace = True
while replace:
for i in listt:
if i in str:
print('y')
replace = False

Building a function to count leading zeros in a string with no success [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
This is what I came up with:
def leading_zeros_counter(x: str):
zeros = 0
for i in x:
if i == str(0):
zeros += 1
else:
return int(zeros)
It seems to work on all strings unless the string contains all zeros (e.g. '0000' does not return '4'). Can anyone explain why and how to fix this?
You need to add a return statement out of the if condition:
def leading_zeros_counter(x: str):
zeros = 0
for i in x:
if i == "0":
zeros += 1
else:
return zeros
return zeros
print(leading_zeros_counter("0000"))
So the output would be:
4

Why I can't get an index from list filled with Ints? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So, it gives me
AttributeError: 'int' object has no attribute 'index'
length = int(input())
arrayOfStrings = input()
number= 0
arrayofnumbers = list(map(int, arrayOfStrings.split()))
minimum = arrayofnumbers[0]
for i in range(1, len(arrayofnumbers)):
if minimum<arrayofnumbers[i]:
number = arrayofnumbers[i].index(i) ErrorString
minimum = arrayofnumbers[i]
elif minimum==arrayofnumbers[i]&number<i :
number = i-1
i+=1
print(number)
There are some errors:
elif minimum==arrayofnumbers[i]&number<i you have to do an AND logic here, so use and instead of &.
arrayofnumbers[i].index(i) arrayofnumbers[i] is an integer, so you have to cast into a str before use "index" method
Why i+=1? For loop increment i by default in range
Use built-in min() method 'cause it's better if you want to find the minimum element of an array:
min(arrayofnumbers)
arrayofnumbers[i] is an integer, not a string or object. So, you can not index into integer value. instead you can modify error line to number = arrayofnumbers.index(i), because list supports the indexing.

Categories

Resources