+ 2
can this be simplified?
please comment if any better code is possible https://code.sololearn.com/cQ7C7ZU0qRtx/?ref=app
12 Answers
+ 2
this is better Imadez
num = int( input() )
while num > 9:
dig = sum( int( c ) for c in str( num ) )
num = dig
print( num )
+ 4
Just like this:
num = int( input() )
while num > 9:
num = sum( int( c ) for c in str( num ) )
print( num )
+ 3
Harsha S ,
there is no need to use a while loop. have a look at this, it is based on your code:
num = input()
print(sum( int( c ) for c in num))
+ 3
Harsha S ,
ok, i understand. under this circumstance the loop is needed.
+ 2
Try this:
https://code.sololearn.com/cA229A10A14a
+ 2
Harsha S How about this one? :-
(Edited)
print((k := lambda x: x if x < 10 else k(sum(int(i) for i in str(x))))(int(input())))
# Hope this helps
+ 1
You can remove line 10: "num = dig"
+ 1
Lothar read the docstring
I need the input to be reduced to a single digit
+ 1
Calvin Thomas
This is more precise
print((k := lambda x: x if x < 10 else k(sum(int(i) for i in str(x))))(int(input())))
0
it doesn't work
0
yeah it works tq
0
Harsha S Oh, I just forgot to cast the input into an integer. Thank you for the correction.