0
Can some explain this code explicitly. Like how the previously entered digits are added tg in 8th line what is the use of num_1
3 Réponses
+ 3
Everything inside the while True loop will continue for ever until it encounters a break.
The only way it can break is if you enter something that is not a digit.
So long as you enter digits, they are stored in num_1, whereafter they get added.
Unfortuanately it is text addition and not integer addition.
if you want to end this loop you have to enter a non digit.
In SoloLearn the entry method is a bit wonky.
So you have to enter all the numbers at the same time, seperated by <enter>
3 <enter>
4 <enter>
5 <enter>
X #Any non digit.
The program experiences it as if it is entered one at a time.
The output would be text addition ( which I believe is not the intent, otherwise you could have entered
345 <enter>
X
Output:
345.
+ 2
'''
I could go into detail, but the summary is it goes against my grain
Rather than trying to understand something that is sub par lets present a more pythonic way.
It seems like you are trying to enter an undefined number of numbers and add them up.
Enter the numbers seperated by space:
3 4 5
output: 12
'''
num=[int(i) for i in input().split(' ')]
print(sum(num))
0
No I just want to understand the code, I don't understand what is the use of num_1 in line 8, I mean does it input new digits or does it add strings with previously entered digits