Do u know how to work on multiple line inputs in python ? | SoloLearn: Learn to code for FREE!


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 .

3/3/2021 10:19:01 AM

Saeed Hayati

7 Answers

New Answer


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


Saeed Hayati Are you doing this in SoloLearn Playground or somewhere else (in IDE)?


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)


Have you tried using exception handling yet?


It's not in the sololearn .


My problem is , I can't get more than one input , and the only input has multiple lines.


Did you mean like this?