+ 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

13th Jan 2018, 10:02 PM
mYstic
mYstic - avatar
30 Respostas
+ 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
13th Jan 2018, 10:05 PM
visph
visph - avatar
+ 1
Before the loop you assign 0 to 'b', but first line of loop, you try to assign the value of 'int' to 'b'...
13th Jan 2018, 11:30 PM
visph
visph - avatar
+ 1
So, you must write: b = int(b);
13th Jan 2018, 11:48 PM
visph
visph - avatar
+ 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
13th Jan 2018, 11:58 PM
visph
visph - avatar
+ 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 ^^
14th Jan 2018, 12:03 AM
visph
visph - avatar
+ 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 ;)
14th Jan 2018, 12:04 AM
visph
visph - avatar
+ 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 ;)
14th Jan 2018, 12:06 AM
visph
visph - avatar
+ 1
n -= 1 # is equivalent to: n = n-1
14th Jan 2018, 12:22 AM
visph
visph - avatar
+ 1
That's the goal ^^ If you keep your loop infinite, it will not work in code playground ;)
14th Jan 2018, 12:24 AM
visph
visph - avatar
+ 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 ^^)
14th Jan 2018, 12:43 AM
visph
visph - avatar
+ 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)
14th Jan 2018, 12:47 AM
visph
visph - avatar
+ 1
I've edited my previous post about 'parseInt()' with 'int()': I still was confusing between JS and Python ;)
14th Jan 2018, 12:49 AM
visph
visph - avatar
0
But i put before the loop that it = 0
13th Jan 2018, 11:23 PM
mYstic
mYstic - avatar
0
No im trying to make it a integer
13th Jan 2018, 11:47 PM
mYstic
mYstic - avatar
0
This isnt java so do i need the ;
13th Jan 2018, 11:54 PM
mYstic
mYstic - avatar
0
I think i got it
14th Jan 2018, 12:00 AM
mYstic
mYstic - avatar
0
The problem is it adds everything weird
14th Jan 2018, 12:01 AM
mYstic
mYstic - avatar
0
I think thats sl's fault not py
14th Jan 2018, 12:01 AM
mYstic
mYstic - avatar
0
Can you explain how to do that and it works now it just creates an error at the end
14th Jan 2018, 12:04 AM
mYstic
mYstic - avatar