counting digits of a number by using for loop in python #8729
Replies: 2 comments 1 reply
-
Here are the changes that have been made:
The for loop has been replaced with a while loop. Since the number of
iterations is not known in advance, a while loop is more appropriate in
this case.
The value of r has been initialized to n instead of 1 to ensure the loop
runs at least once.
The indentation of the code inside the loop has been fixed.
The i = i + 1 statement has been moved inside the loop, so that it gets
executed whenever r is greater than or equal to 10.
The output has been modified to display the result using print.
Now, the code will calculate and print the number of digits in the input
number correctly.
n = int(input('Please enter an integer number: '))
i = 0
r = n
while r >= 10:
r = r // 10
i = i + 1
print("The number of digits in", n, "is", i+1)
…On Sun, May 14, 2023, 12:28 AM nazanintalebi ***@***.***> wrote:
HI, here are my codes : n=int(input('please enter an integer number: '))
i=0
r=1
for i in range(0,r):
r=n//10
if r>=1:
i=i+1
print(i)
It didn't work
I can't figure it out
please, help
—
Reply to this email directly, view it on GitHub
<#8729>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMTMV5B2ZYJKRSD33M7LPU3XF7KT3ANCNFSM6AAAAAAYAU5XP4>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
-
#empty list #Input #incrementing due to range function property for num in range(1, Value) : #printing the values on the list #counting the digit of the numbers |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
HI, here are my codes :
n=int(input('please enter an integer number: '))
i=0
r=1
for i in range(0,r):
r=n//10
if r>=1:
i=i+1
print(i)
It didn't work
I can't figure it out
please, help
Beta Was this translation helpful? Give feedback.
All reactions