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
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
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 None
when 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')
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"))
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')
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
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
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 None
when 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')
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"))
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')