0

this is my code:

r = int

for q in range(0 + (casovaJednotka - celkovaCasovaJednotka), casovaJednotka + 1):
    r[q] = q + 1
    q = q + 1

for i in range(casovaJednotka, 0, -1):
    for w in range(0 + (casovaJednotka - celkovaCasovaJednotka), casovaJednotka):
        if r[w] != i:
            print(q[w])
        if casovaJednotka - 1 == w:
            print("0\n")

and this is the problem:

if r[w] != i:
TypeError: 'type' object is not subscriptable

and also it has the same problem with

r[q] = q + 1
5
  • 1
    r is not array, so you can't use r[q]. That's the meaning of the error you reported. Commented Aug 30, 2022 at 10:24
  • 1
    r = int is setting r to the type int Commented Aug 30, 2022 at 10:24
  • 1
    What do you think r = int does? Commented Aug 30, 2022 at 10:25
  • so how do I use indexes with r as int? Commented Aug 30, 2022 at 10:28
  • @ChristoferKiňo I have modified the indentation to make it valid python as your error didn't seem to be syntax related. Feel free to revert if it no longer matches your code. Commented Aug 30, 2022 at 10:31

1 Answer 1

1

In your code, you are initializing the r variable as an int object. By looking at your code, I can assume that you want r to be an array of integers. So instead of defining r=int, you can do this.

r=[]

OR

r = list(range(10)) #initialize a list of size 10 items 

To know more about what is TyprError and Python throw this error (Python Error "TypeError: 'type' object is not subscriptable), you can read this article, here, I have elaborate on this error in detail.

Sign up to request clarification or add additional context in comments.

4 Comments

if I inicialise r = [] I still have porblem with q
for r=[], you need to use r.append(q + 1) but for r= list(range(10)) you can use r[q]=q + 1
now I have problem with q in this: for i in range(casovaJednotka, 0, -1): for w in range(0 + (casovaJednotka - celkovaCasovaJednotka), casovaJednotka): if r[w] != i: print(q[w]) if casovaJednotka - 1 == w: print("0\n") it do not want print q[w]
q[w] is an illegal statement becaue q is an integer object. You first need to convert it into string to perform the indexing operation use this statement int(str(q)[w])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.