Do u know how to work on multiple line inputs in python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Do u know how to work on multiple line inputs in python ?

For example , we don't know the count of input lines , and we have to calculate the sum of entered inputs . That's just one input with multiple lines .

3rd Mar 2021, 10:19 AM
Saeed Hayati
Saeed Hayati - avatar
7 Answers
+ 2
You can define an "exit sequence". If the exit sequence is input, the loop stops. If you are reading numbers you can take any value that can't be converted
3rd Mar 2021, 10:27 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Saeed Hayati Are you doing this in SoloLearn Playground or somewhere else (in IDE)?
3rd Mar 2021, 10:46 AM
Ipang
+ 2
Abir Hasan I don't think that's what he meant. But exception handling like you did is probably the best approach. Saeed Hayati you can do something like this sum = 0 while True: try: sum += int(input()) except ValueError: continue except EOFError: break print(sum) This code ignores input lines that can't be cast to integer (ValueError)
3rd Mar 2021, 10:54 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Have you tried using exception handling yet?
3rd Mar 2021, 10:51 AM
Ipang
0
It's not in the sololearn .
3rd Mar 2021, 10:50 AM
Saeed Hayati
Saeed Hayati - avatar
0
My problem is , I can't get more than one input , and the only input has multiple lines.
3rd Mar 2021, 10:57 AM
Saeed Hayati
Saeed Hayati - avatar
3rd Mar 2021, 10:35 AM
Abir Hasan
Abir Hasan - avatar