 Number of zeros at the end | Sololearn: Learn to code for FREE!

+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?

+5

#try this for i in list: if i == '0': number_of_zero +=1 else: break print(number_of_zero)

+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)

+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

+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 '''

+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

+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.

+1

Thank you very much. I reverse the list and use this. And it's finally working.

+1

Yes. Thank you very much.

+1

I just tried the task. https://code.sololearn.com/ctfQdSKx2HwL

+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

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?

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.

0

0

Prabhas Koya your “idea” is awesome, especially using break statement for stopping “counting” nums of zeros!

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)