25 lines
475 B
Python
25 lines
475 B
Python
|
#!/usr/bin/env python
|
||
|
|
||
|
"""Algorithme : Somme des chiffres d'un entier N donné"""
|
||
|
|
||
|
|
||
|
def main():
|
||
|
print(SommeChiffres("123"))
|
||
|
|
||
|
|
||
|
def SommeChiffres(N : str, somme: int = 0) -> int:
|
||
|
nombre = [int(n) for n in N]
|
||
|
try:
|
||
|
chiffre = nombre[0]
|
||
|
except IndexError:
|
||
|
return somme
|
||
|
else:
|
||
|
somme += chiffre
|
||
|
nombre.pop(0)
|
||
|
N = "".join(str(n) for n in nombre)
|
||
|
return SommeChiffres(N, somme)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|