+ 1
I need help
Can someone tell me why this code doesn't work and how to fix it https://code.sololearn.com/cRfmKDBp8Gdy/?ref=app
30 Answers
+ 2
b=0
while 1:
b=int
a=float(input())
b=float(b)+float(a)
print(b)
# in the first line of the while loop, you try to assign 'b' variable with value of 'int', wich is not previously declared
+ 1
Before the loop you assign 0 to 'b', but first line of loop, you try to assign the value of 'int' to 'b'...
+ 1
So, you must write:
b = int(b);
+ 1
In javascript the semi-colon is often not mandatory, but it's a good practice to use it where it should go, to avoid unwanted bugs due to the way the interpreter will try to add them for you ^^
Example:
function test() {
return
42
}
You could think that's a valid code, and in a way, it is... but if you call it, it will return 'undefined' because interpreter will add a semi-colon after the return statement ;P
+ 1
Your code is:
b=0
while 1:
a=float(input())
b=float(b)+float(a)
print(b)
This will do an infinite loop calling the print API of the browser (to output at a printer)...
You need to rather use alert() function, and have a base case to end your loop ^^
+ 1
My half-bad:
print() is the right function to use in Python context (I was confusing with another thread about JS), but you must have a base case anyway ;)
+ 1
n = 3
b = 0
while 1:
a=float(input())
b=float(b)+float(a)
print(b)
n -= 1
if n == 0:
break
# more than one space for indentation improve readability ;)
+ 1
n -= 1
# is equivalent to:
n = n-1
+ 1
That's the goal ^^
If you keep your loop infinite, it will not work in code playground ;)
+ 1
Assign 1000 to n, so the loop will run 1000 times...
But in sololearn code playground, you will need to input all 1000 expected values at once, each at a new line in the prompt box opened when you run the code (and with 1000 numbers, you'll probably get a time limit reached error in code playground ^^)
+ 1
# If you don't know the number of input, you can do something like:
b = 0
while 1:
try:
a=input()
except:
a=''
if a == '':
break
a=float(a)
b=float(b)+float(a)
print(b)
# so the loop will break when you enter an empty input, as well as in code playground when there's no more line available in the sololearn main input (on EOF error)
+ 1
I've edited my previous post about 'parseInt()' with 'int()': I still was confusing between JS and Python ;)
0
But i put before the loop that it = 0
0
No im trying to make it a integer
0
This isnt java so do i need the ;
0
I think i got it
0
The problem is it adds everything weird
0
I think thats sl's fault not py
0
Can you explain how to do that and it works now it just creates an error at the end