+ 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
20 Antworten
+ 8
sum = 0
while True:
x = input()
if x == "stop":
break
sum += int(x)
print(sum)
print(sum)
+ 4
You can take x as string input and cast it to integer using int() in addition.
+ 3
Flow of control. The int() is encountered before 'if...break' and it raises error. , Because int() can't function on strings like 'stop'
+ 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)
+ 2
Yyyyyyyyyyyyyyy😀😀😀😀😀😀
+ 1
In the while loop you could first test if == „stop“ and handle this as you that did below.
+ 1
x = input()
sum = sum + int(x)
And try using if-else statement instead.
+ 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
+ 1
You only need to print sum outside loop as a final value.
And, do calculations after if block.
+ 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
+ 1
why if block before sum = sum+int(x)?
+ 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
0
like this
x = int(input())
x = input()
?
0
x = int(input())
y = input()
is this possible?
0
Porque un int no debe recibir string
0
sum =0
while True:
x = input()
if x.isnumeric():
sum +=x
else:
if x == "stop"
break
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
0
I need to make some $$
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 .