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.
Thank you so much I’m studying your code to better understand.
– SrTecc