+ 2

Number of zeros at the end

Hello, can anyone tell me why this ist working? I have problem with while and return. number = 1802300 string_number = str(number) list = list(string_number) list.reverse() for i in range (len(list)): number_of_zero = 0 while i == 0: number_of_zero +=1 return number_of_zero It's telling me, that I have return outside function. But I don't know why?

13th Nov 2021, 9:16 AM
Jeanie Snow
Jeanie Snow - avatar
15 Respostas
+ 5
#try this for i in list: if i == '0': number_of_zero +=1 else: break print(number_of_zero)
13th Nov 2021, 10:13 AM
Simba
Simba - avatar
+ 5
#do you mean number of zero's at the end? #If yes, then another way using while loop : num = 1802300 number_of_zero =0 while not num%10: number_of_zero +=1 num=num/10 print(number_of_zero)
13th Nov 2021, 11:01 AM
Jayakrishna šŸ‡®šŸ‡³
+ 3
THIS IS WHAT YOU INTENDED TO DO. 1) return statement is for functions not for the loops, hence you are getting error https://code.sololearn.com/cSK5ICH4UiLm/?ref=app
14th Nov 2021, 3:44 AM
Prabhas Koya
+ 3
#I can add another way Str = input() print(len(Str)-len(Str.rstrip('0'))) ''' *using built-in String methods *total - removing the last 0 digits and finding length *the same idea '''
15th Nov 2021, 2:09 AM
Prabhas Koya
+ 1
number = 1802300 string_number = str(number) list = list(string_number) list.reverse() #you are dealing with string then no need to reverse here. you can directly work. number_of_zero = 0 #make this outside of loop. inside loop cause reset to 0 every time for i in list: if i == '0': #you if block, ' while ' is a loop, just like for loop. number_of_zero +=1 print(number_of_zero) # 'return' returns a value from function and comes out function but print() function displays output on screen. #Hope it clears.. else reply..Jeanie Snow
13th Nov 2021, 9:47 AM
Jayakrishna šŸ‡®šŸ‡³
+ 1
Thank you. But I need numbers of 0 after some different numbers. Like for 1802300 I need the numbers of 0 after number 3. This is why I tried reverse the list. Because I wanted just count of 0 before number 3.
13th Nov 2021, 9:54 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
Thank you very much. I reverse the list and use this. And it's finally working.
13th Nov 2021, 10:25 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
Yes. Thank you very much.
13th Nov 2021, 11:13 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
13th Nov 2021, 3:26 PM
Krishnam Raju M
Krishnam Raju M - avatar
+ 1
Axel_ 1) count method counts all zeros not only just the end zeroes(see your code) 2) NUMBER could be anything so slicing by just seeing the number won't be useful
14th Nov 2021, 3:56 AM
Prabhas Koya
0
You have while loop in infinite one. What are you trying with that? If while ,not work then it comes out for loop by return number_of_zero in first iteration. Then why need of loop ..? what the task you trying?
13th Nov 2021, 9:28 AM
Jayakrishna šŸ‡®šŸ‡³
0
I have a number, for example 1300 . And want to tell how many zeros are at the end. So I convert int to str. Like 1300 - > "1300" then I want to separate the letters like "1","3","0","0" reverse them. "0","0","3"1" And then I would like to count the number of 0, which are before 3. Like while number is zero. Count it. Sorry for bad explanation.
13th Nov 2021, 9:38 AM
Jeanie Snow
Jeanie Snow - avatar
0
Thank you very much.
13th Nov 2021, 11:28 AM
Jeanie Snow
Jeanie Snow - avatar
0
Prabhas Koya your ā€œideaā€ is awesome, especially using break statement for stopping ā€œcountingā€ nums of zeros!
14th Nov 2021, 3:54 AM
US Department Of State
US Department Of State - avatar
0
#You can count last zeroes like this also num=1802300 string_num=str(num) c=0 for i in string_num[::-1]: if i=="0": c+=1 else: break print("Zeroes are",c)
14th Nov 2021, 5:19 PM
Ratnapal Shende
Ratnapal Shende - avatar