+ 3

Give me a hint please

i try to solve this problem: You’re making a calculator that should add multiple numbers taken from input and output the result. The number of inputs is variable and should stop when the user enters "stop". my code is: sum = 0 while True: x = int(input()) sum = (sum+x) print(sum) if x == "stop": break print(sum) i understand that "stop" isnt a number, and that is why my code doesnt work. But How to present it as number? give me please a hint

3rd Mar 2022, 4:53 PM
Askar Kondybaev
Askar Kondybaev - avatar
20 ответов
+ 8
sum = 0 while True: x = input() if x == "stop": break sum += int(x) print(sum) print(sum)
3rd Mar 2022, 5:09 PM
JaScript
JaScript - avatar
+ 4
You can take x as string input and cast it to integer using int() in addition.
3rd Mar 2022, 4:56 PM
Simba
Simba - avatar
+ 3
Flow of control. The int() is encountered before 'if...break' and it raises error. , Because int() can't function on strings like 'stop'
3rd Mar 2022, 5:34 PM
Cat
+ 2
Find a way to first process the input as a string for instance while True: x = input() if x=="stop": break x = int(x)
4th Mar 2022, 7:26 AM
Pythøñ Cúltîst⚔️🔥🇳🇬
Pythøñ  Cúltîst⚔️🔥🇳🇬 - avatar
+ 2
Yyyyyyyyyyyyyyy😀😀😀😀😀😀
5th Mar 2022, 8:07 AM
Yunlai Zhu
+ 1
In the while loop you could first test if == „stop“ and handle this as you that did below.
3rd Mar 2022, 5:02 PM
JaScript
JaScript - avatar
+ 1
x = input() sum = sum + int(x) And try using if-else statement instead.
3rd Mar 2022, 5:06 PM
Simba
Simba - avatar
+ 1
sum = 0 while True: x = input() sum = sum+int(x) print(sum) if x == "stop": break print(sum) this one doesn't work, whats wrong
3rd Mar 2022, 5:13 PM
Askar Kondybaev
Askar Kondybaev - avatar
+ 1
You only need to print sum outside loop as a final value. And, do calculations after if block.
3rd Mar 2022, 5:14 PM
Simba
Simba - avatar
+ 1
You need to use if block before using int() function. It's the flow of control. It will raise error when it encounters int() if it's a string
3rd Mar 2022, 5:15 PM
Cat
+ 1
why if block before sum = sum+int(x)?
3rd Mar 2022, 5:30 PM
Askar Kondybaev
Askar Kondybaev - avatar
+ 1
Convert x to an int after checking if it equals 'stop'. You can store it in a variable or just use sum += int(x) Because we don't use the int value of x anywhere else
4th Mar 2022, 4:55 PM
Hiba al-Sayf
Hiba al-Sayf - avatar
5th Mar 2022, 11:41 AM
SIDDHARTH A
SIDDHARTH A - avatar
0
like this x = int(input()) x = input() ?
3rd Mar 2022, 5:03 PM
Askar Kondybaev
Askar Kondybaev - avatar
0
x = int(input()) y = input() is this possible?
3rd Mar 2022, 5:10 PM
Askar Kondybaev
Askar Kondybaev - avatar
0
Porque un int no debe recibir string
5th Mar 2022, 5:55 AM
Felipe Alejandro Rodríguez Romero
0
sum =0 while True: x = input() if x.isnumeric(): sum +=x else: if x == "stop" break
5th Mar 2022, 7:40 AM
east-hill sunrise
east-hill sunrise - avatar
0
olli You cannot get a while loop input to work properly here in Sololearn. while True: x=input() will not work here. All inputs are processed all in one go at the submit popout box. That is why python codes that need repeated inputs like the hangman game or number guessing game are not possible here. Maybe a workaround? https://code.sololearn.com/c3m89XkQAq7G/?ref=app
5th Mar 2022, 9:43 AM
Bob_Li
Bob_Li - avatar
0
I need to make some $$
5th Mar 2022, 9:55 AM
Heather Michelle Green
Heather Michelle Green - avatar
0
Try this sum = 0 x = int(input()) while not x==“stop” : sum+=x x = int(input()) print(sum) Tell my if it’s not work .
5th Mar 2022, 4:51 PM
hanan nassar
hanan nassar - avatar