How to add a sentence before the the number/int PYTHON

Question:

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
Asked By: Meemoz

||

Answers:

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
Answered By: Claudio Paladini

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')
Answered By: Neo

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"))
Answered By: AziMez

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')
Answered By: OldBill
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.