for i in range(0, int(num[pos])): Typeerror: 'int' Object is not subscriptable

Asked

Viewed 140 times

-4

def validar(pergunta, min, max):
    x = int(input(pergunta))
    while((x < min) or (x > max)):
        print('Código Inválido')
        print('Digite um número inteiro entre 10000 e 30000')
        x = int(input(pergunta))
    return x

def calcular(num):
    print('Dígito          |', end='')
    pos = 0
    while pos < 5:
        for i in range(0, int(num[pos])):
            print(f'{i} |', end='')
            pos += 1
    print()
    print('Peso            |', end='')
    for peso in range(2, 7):
        print(f'{peso:^5} |', end='')
    print()
    print('Multiplicação   |', end='')
    soma = 0
    peso1 = 2
    resto = 0
    for d in range(0, 5):
        for digito in num:
            resultado = int(digito[d]) * peso1
            soma += resultado
            peso1 += 1
            resto = soma % 7
            print(f'{resultado:^5} |', end='')
    print(f'   Soma todos = {soma}')
    print('-' * 75)
    print(f'                                                     Resto de {soma} por 7 = {resto}')
    print('-' * 75)
    print(f'{int(num[0])} - {resto}')
    print('------')
    return f'{num}-{resto}'

digito = validar('Digite o código do produto: ', 10000, 30000)

calcular(digito)
  • 1

    Please explain exactly what your question is and how we can help you. Don’t just play the code in a question.

  • 1

    To know better how to ask a question, click here.

  • I voted to close as needing more details, but I was wrong. The correct closing vote is due to quality. In function validar() the return is always a whole x = int(input(pergunta)) which implies that on the line for i in range(0, int(num[pos])): an error will always be generated because num is the return of validar() and being a whole cannot be sliced num[pos]. The problem can be easily detected doing the code debugging.

  • Not sure what you want to do, but looking at your code I think you’re trying to do this https://ideone.com/Nnt81K

1 answer

0


In the code for i in range(0, int(num[pos])):, more specifically here: int(num[pos]), you are trying to access position 0 of an integer number, in case that number is your variable num.

That mistake (Typeerror: 'int' Object is not subscriptable), According to the python documentation, it occurs when you try to access a function as if it were an eternal object. In case, you are trying to treat this whole number as if it were a Array, but it doesn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.