+ 2

can this be simplified?

please comment if any better code is possible https://code.sololearn.com/cQ7C7ZU0qRtx/?ref=app

29th Jul 2021, 6:22 PM
Harsha S
Harsha S - avatar
12 Respuestas
+ 2
this is better Imadez num = int( input() ) while num > 9: dig = sum( int( c ) for c in str( num ) ) num = dig print( num )
29th Jul 2021, 7:07 PM
Harsha S
Harsha S - avatar
+ 4
Just like this: num = int( input() ) while num > 9: num = sum( int( c ) for c in str( num ) ) print( num )
29th Jul 2021, 7:52 PM
Imadez
Imadez - avatar
+ 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))
30th Jul 2021, 12:32 PM
Lothar
Lothar - avatar
+ 3
Harsha S , ok, i understand. under this circumstance the loop is needed.
30th Jul 2021, 1:02 PM
Lothar
Lothar - avatar
29th Jul 2021, 7:06 PM
Imadez
Imadez - avatar
+ 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
31st Jul 2021, 2:49 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
You can remove line 10: "num = dig"
29th Jul 2021, 7:19 PM
Imadez
Imadez - avatar
+ 1
Lothar read the docstring I need the input to be reduced to a single digit
30th Jul 2021, 12:55 PM
Harsha S
Harsha S - avatar
+ 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())))
31st Jul 2021, 3:03 PM
Harsha S
Harsha S - avatar
0
it doesn't work
29th Jul 2021, 7:26 PM
Harsha S
Harsha S - avatar
0
yeah it works tq
29th Jul 2021, 7:57 PM
Harsha S
Harsha S - avatar
0
Harsha S Oh, I just forgot to cast the input into an integer. Thank you for the correction.
31st Jul 2021, 3:11 PM
Calvin Thomas
Calvin Thomas - avatar