Where is my mistake? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Where is my mistake?

I want to input numbers and add them, but if I write stop the program stops and prints the total. sum = 0 while True: x = input() #your code goes here if str(x)=='stop': print(stop) else: continue sum+=int(x) print(sum)

13th Mar 2021, 4:01 PM
Michael Lazos
Michael Lazos - avatar
3 ответов
+ 2
Your whole if block should be indented under the while loop. Why are you printing a variable 'stop' which doesn't exist? Use the break statement to break out of the loop. Remove the continue statement from the else block because if you skip the iteration, when are you gonna add the numbers? sum = 0 while True: x = input() #your code goes here if x == 'stop': break else: sum += int(x) print(sum)
13th Mar 2021, 4:05 PM
Soumik
Soumik - avatar
+ 1
Not sure why would you use continue , your if else are outside while loop and, continue works inside a loop only. Following should be the correct code, sum = 0 while True: x = input() #your code goes here if str(x)=='stop': print("stop") break else: sum+=int(x) print(sum)
13th Mar 2021, 4:06 PM
Abhay
Abhay - avatar
0
thank you
13th Mar 2021, 4:07 PM
Michael Lazos
Michael Lazos - avatar