0

'break' problem

Hi everyone. I tried to write a program on sololearn platform that takes input as integer but breaks when I input "stop" which is a string. Here is the error I'm having: Traceback (most recent call last): File "/usercode/file0.py", line 4, in <module> x = int (input()) ValueError: invalid literal for int() with base 10: 'stop' This is my code" i = 1 sum = 0 while True: x = int (input()) #your code goes here sum = sum + x print (sum) i = i + 1 if x == "stop" : break Can someone pls point out where the problem is or what can I do?

15th Dec 2021, 11:40 AM
Oluwaseunfunmi Adeniji
5 Réponses
+ 2
sum = 0 while True: x = input() # don't convert <x> to int yet, we need <x> as string to check for 'stop' if x == "stop": break # now convert and add <x> to <sum> sum = sum + int(x) print (sum) Please tag a programming language relevant to the question (Python) up there where it says 'urgent' https://code.sololearn.com/W3uiji9X28C1/?ref=app
15th Dec 2021, 1:42 PM
Ipang
+ 1
Ipang.... your code is showing EOF error for some reason.
16th Dec 2021, 5:40 AM
Sidharth SK
Sidharth SK - avatar
+ 1
Sidharth, The last input given must be 'stop' otherwise it will try to read more input, but there may not be any more input ...
16th Dec 2021, 5:44 AM
Ipang
+ 1
Thanks, everyone
20th Dec 2021, 1:24 PM
Oluwaseunfunmi Adeniji
0
try this:- sums = 0 i = 0 while True: try: x = eval(input("")) #your code goes here if x != "stop" : sums = sums + x print (sums) i = i + 1 else: break except: break it von't iterate on an infinite level though...
15th Dec 2021, 12:02 PM
Sidharth SK
Sidharth SK - avatar