Write a python script that reads an 8-digit integer

Asked

Viewed 3,158 times

1

Write a program that reads an 8-digit integer number. The output of your program must be the sum of all digits of the given integer. If the number typed does not have 8 digits, the program must write 'NAO SEI' (without accents and quotation marks).

IMPORTANT: You should solve this problem using repeat structure. Otherwise, the note will be zero.

Exemplo 1:

Entrada:

123

Saída:

NAO SEI



Exemplo 2:

Entrada:

34576890

Saída:

42

Guys I made the following code but my note is still 5 of 10 and I think the problem is in this part of:

 if n<=1:
    print("NAO SEI")

because the program wants that if the number typed does not have 8 digits, the program should write 'DO NOT KNOW'

n=int(input("valor de n:"))

soma=0

while n>0:

resto=n%10

n=(n-resto)//10

soma=soma+resto

print(soma)

if n<=1:

print("NAO SEI")

2 answers

4

The best way to solve it is:

numero = input('Número: ')

if len(numero) == 8:
    print(sum(map(int, numero)))
else:
    print('NAO SEI')

Which could even be reduced to:

numero = input('Número: ')

print(sum(map(int, numero)) if len(numero) == 8 else 'NAO SEI')

Without prejudice to the legibility and semantics of the code. But, as you explicitly ask to use repetition loops, which, probably to train such a structure, you can solve as follows:

algarismos = input('Número: ')

if len(algarismos) == 8:
    numero = int(algarismos)
    soma = 0
    while numero > 0:
        soma += numero % 10
        numero = numero//10
    print(soma)
else:
    print('NAO SEI')

See working on Repl.it

Basically the only difference to your code, disregarding the indentation errors in the question, is that I checked the number of digits of the number while it was still a string, on the return of input.

The question to be raised is: the number "01234567" should be considered as 8 digits or 7? If 8, the treatment with string is the simplest, but, if it is 7, the number should be the same numerical type, so the ideal would be to check if it is within the range [100000000, 99999999]:

numero = int(input('Número: '))

if 10000000 <= numero <= 99999999:
    soma = 0
    while numero > 0:
        soma += numero % 10
        numero = numero//10
    print(soma)
else:
    print('NAO SEI')

See working on Repl.it

0

Another way to resolve this issue is by using the function concepts along with the repetition loop for.

One of the ways we can implement the code is:

def soma_digitos(n):
    if len(n) == 8:
        return sum([int(i) for i in n])
    return 'NAO SEI'


if __name__ == '__main__':
    numero = input('Digite um número: ')
    resultado = soma_digitos(numero)

    print(f'{resultado}')

Note that this code captures the entered value in the form of string, passes this value, as parameter, to the function soma_digitos(). Once there, the string size is checked. If this size is equal to 8, another list will be assembled, in which each of its elements is an integer and then with the help of the function sum, the sum of its values will be calculated and the return will be displayed later.

Now, if the list size is different of 8 the return displayed will be the message I DON’T KNOW.

Browser other questions tagged

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