Help with PYTHON lists

Asked

Viewed 160 times

-3

I’m doing a college paper, but I’m having a hard time finishing a code.

Image of the activity:

inserir a descrição da imagem aqui

I’m trying to make a list. Yeah, I think that would be an effective way to solve this exercise. However, I would need to calculate 1 by 1 of the numbers, but I don’t know how to do that.

Code I’m trying to make:

L = []
P = []
C = int(input("Digite o código de 5 digitos: "))
P2 = int(input("Digite o peso (5 digitos): "))

L.append(C)
P.append(P2)


print(f"Codigo {L} e peso {P}")

2 answers

0


1 - The while controls the entry between 10000 and 30000.

2 - Convert variable to string to access digits separately.

3 - Place in the vaiavel resultado each digit separately.

4 - Place in the vaiavel resultadoM the digits produdo by weight.

5 - Take the rest of the division from soma by 7:

numero = int(input("Digite o código de 5 digitos: "))
while numero < 10000 or numero > 30000:
  numero = int(input("o numero deve ser entre 10000 e 30000: "))

resultado = []
resultadoM = []
digitoV = 0
soma = 0
for peso, valor in enumerate(str(numero), start = 2):
  resultado.append(int(valor))
  resultadoM.append(int(valor) * peso)
soma = sum(resultadoM)
digitoV = soma % 7

print('Numero ' + str(resultado))
print('Multiplicado: ' + str(resultadoM))
print('Soma: ' + str(soma))
print('Digito: ' + str(digitoV))
  • Thank you so much I’m studying your code to better understand.

0

The statement requests a FUNCTION that returns only the DIGIT VERIFIER according to certain rules.

One of the ways we can assemble this function is:

def digito_verificador(n):
    soma = 0
    o, p = str(n), list(range(2, 7))
    for i, j in zip(o, p):
        soma += int(i) * j
    return soma % 7


while not (num := input('Digite um número entre 10000 e 30000: ')).isdigit() \
        or (num := int(num)) < 10000 or num > 30000:
    print('Valor INVÁLIDO!')

print(digito_verificador(num))

Once the code is initialized, the entered value is captured and, with the help of the Assignment Expression, treated and exhausted by PEP 572 three consecutive checks shall be carried out.

The first check assesses whether the expression assigned to the variable num NAY is a digit. The second, evaluates whether to convert the variable num whole is less than 10000 and the third, assesses whether such a conversion is greater than 30000.

If one of those three checks is True, we will receive the mensgame INVALID VALUE! and we will again be asked a value.

If all three checks are False the tie while is closed, sent the variable value num as a parameter for the function digit_checker(). Once there, this value will be converted to Sting. Then the block is, with the help of function zip() will traverse the interactor "o" - which is the value converted into string - and the list formed by the weights - which is generated by the range(2,7).

For each interaction of the for block the value of "int(i) * j" generating a partial sum.

After having performed all the appropriate interactions generating the total sum, the rest of the division between sum and 7 is calculated and displayed, i.e., sum % 7.

OBSERVING:

the character " \ " after the method isdigit() is used to perform a line change without leaving the context of the current line, i.e., after the " \ " we can continue typing the commands in the line immediately below referring to the same while command line.

Testing the code:

Imagine that we want to calculate the value checker digit 21853. For when executing the code we must enter...

21853

...and press Enter.

From that moment the calculations will be made and we will receive as output the value:

5

Note that the return of the function is only the check digit.

Browser other questions tagged

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