0

Here's the code i did:

list = input("Enter the number: ")

p = print("Trailing zeroes = ")

print(p, list.count("0"))

Output:

Enter the number: 10000

Trailing zeroes = 

None 4

The output i wanted:

Enter the number: 10000

Trailing zeroes = 4
deceze
  • 510,633
  • 85
  • 743
  • 889
Meemoz
  • 13
  • 1

4 Answers4

1

To count trailing zeroes you could do this:

num = input('Enter number: ')

ntz = len(s)-len(s.rstrip('0'))

print(f'Your input has {ntz} trailing zeroes')
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

Here is a working example of the code:

i = input("Enter the number: ")

p = "Trailing zeroes = " + str(i.count("0"))

print(p)

Output:

Enter the number: 1000
Trailing zeroes = 3
Claudio Paladini
  • 1,000
  • 1
  • 10
  • 20
  • The question itself is "add sentence before number". So I think the essence of it is more pasting/concatenating strings and numbers AND not using `print` to store strings. Correct me if I worng @Meemoz – Claudio Paladini Oct 20 '22 at 11:45
0

Taking into account @OldBill 's comment & answer: I corrected the code so that it gives an answer taking into account both of Op's problem. The counting of the trailing zero is @OldBill 's code and not mine.

With

list = input("Enter the number: ")

p = print("Trailing zeroes = ")

print(p, list.count("0"))

What you do is p = print("Trailing zeroes = ") which is not a value. That explains the Nonewhen you print p. Plus your counting of trailing zero doesn't work, and count all zeroes.

To have your code working, either you should have

num = input('Enter number: ')

ntz = len(num)-len(num.rstrip('0'))

print(f'Your input has {ntz} trailing zeroes')

or

num = input('Enter number: ')

print('Your input has', len(num)-len(num.rstrip('0')),' trailing zeroes')
Neo
  • 525
  • 3
  • 17
  • What output do you get for an input of 101000? Note that this value has 3 trailing zeroes – DarkKnight Oct 20 '22 at 09:44
  • Thanks for the comment, didn't really take this in account. I edited to give a more complete answer. – Neo Oct 20 '22 at 10:01
0

Another way to do this:

NB1: the input is a string not a list.

NB2: This will count all zeroes - not just trailing – (By @OldBill)

text= input("Enter the number: ")

print("Trailing zeroes = ", text.count("0"))
AziMez
  • 2,014
  • 1
  • 6
  • 16
  • 1
    @OldBill, thank you for this note, I'll update it. However, the *OP* Question is about **How to add a sentence before the number?** – AziMez Oct 20 '22 at 09:44