0
Loop in python
Hi, the question is to write a program that gets numbers and return their sum, the loop will run unknown times until the number -999. There's no output, also I didn't understand why num is divided by 10 (and not 100 for example). https://code.sololearn.com/cVJ802Y4i8Xj/?ref=app
14 odpowiedzi
+ 7
Liat123 ,
the first 3 lines are creating an infinite loop. check the loop conditions.
+ 5
num is not defined Liat123 so that is one error as well as the indentions mentioned by Jayakrishna🇮🇳
+ 3
num is undefined. You may supposed to be use x instead of num. Or assign x into num before using it. Like num=x
last line has no meaning if it not indented inside loop..
Without indentation 2nd loop not belong to outer first loop..
indentation is adding proper spaces before a instruction so that same consequent indentation code belong to same block..
see this code for indentation mistakes:
https://code.sololearn.com/cT5BRIbkia21/?ref=app
https://www.sololearn.com/discuss/3096382/?ref=app
+ 2
Liat123
You may understand now.. Compare this code with yours. You can ask if anything is not understood... look at spaces before statements....
x=0
while x != -999:
s=0
while (x!=0):
s+=1
x//=10
print(s)
x = int(input("-999 to stop"))
+ 1
Is it not your code ?
It's has logic errors..
Also indentation mistakes, cause infinite loop.. So no output.
Divide 10 => remove a digit from number.
Ex:
1234//10 = 123
123//10 = 12
12//10 =1
1//10 = 0
+ 1
That's the solution of the question, maybe it's wrong.
The original solution's last line was: "x = int(input(“more? (-999 to stop):”)"
I deleted the "more?" cause there was an error.
Where are the indentation and logic errors?
Thanks!
+ 1
I changed num to x
When I tried to put the second loop under the first one, there was an indentation error
I don't know what else doesn't work, indentation looks ok
+ 1
Jayakrishna🇮🇳 I copied your code and saved in my original code, but still no output.. Sorry for all the questions
+ 1
Jayakrishna🇮🇳 I updated my code like yours. I put input 5 or -999 - doesn't matter what, still no output.
I just read the question again - the output should be the number of digits of the number I put (5 is 1, 10 is 2, etc).
Sorry for the confusion.
"write a program that gets a number and return the numbers of its digits. The program will get unknown amount of numbers until it'll get -999"
Sounds like an infinite loop but there's a stop at a number. So idk..
+ 1
My code has indented last 2 lines as come into first while loop.. but you not noticed it.. look again.
Trh not to change original code in question. Do share update code with other new one always for clear discussion..
Like this:
edit: Liat123
https://code.sololearn.com/cXnTPf3reGtM/?ref=app
+ 1
Jayakrishna🇮🇳 Oh OK, I thought it's better to update the original code so it won't be confusing or too long at the comments.
I ran your code, every number I put gets the output 0, number of digits and then there's a EOF error on the last line.. Don't know why. Why there's 0 output before the count of digits?
The number -999 gets output 0 and then stop. Any other number gets 0, the count of digits and then EOF error
+ 1
Liat123
Let's take this input sample :
1234
234
543
-999
Output you get:
0 // initially x = 0
-999 to stop
4 // x = 1234 , 4 digits
-999 to stop
3 // x = 234 => 3 digits
-999 to stop
3 // x = 543 => 3 digits
-999 to stop
Loop exits since -999 is next input.
As per sololearn input handling, you need to give all required input at once by line by line, in pop-up window before running as it is not interactive. So try my sample by just copy paste.
As I already told, I just edited your code to run just without errors.
0
Lothar that's the exercise, the loop will run unknown times until the number -999..
0
Liat123
am getting output. It is as per code may not be your expected output. As you not told your task clearly.. As per description comparing, it won't return sum. But returns number of digits in a number.
Can you share link of new your update code?
what are your input and expected output....?