Respuesta :
Answer:
Step-by-step explanation:
We can get this done by using the code
def digits(n):
count = 0
if n == 0:
return 1
while (n > 0):
count += 1
n= n//10
return count
Also, another way of putting it is by saying
def digits(n):
return len(str(n))
------------------------------------------
print(digits(25)) # Should print 2
print(digits(144)) # Should print 3
print(digits(1000)) # Should print 4
print(digits(0)) # Should print 1
Doing this way, we've told the system to count the number of figures that exist in the number. If it's 1000 to 9999, then it records it as 4 digits. If it's 100 - 999, then it records it as 3 digits. If it's 10 - 99, it records as 2 digits. If it's 0 - 9, then it has to record it as a single digit.
Thanks
Following are the digits function that calculte the digits:
Program Explanation:
- In this question, we define two in which one use predefine methods and one is used for calculate value by logics, which can be defined as follows:
- Defining a method "digits" that takes "n" variable inside the parameter.
- Inside the method "x"variable that converts parameter value into string and counts its length value and use return keyword that return the length of x.
- Outside the method, a variable n is define that inputs value, and call the method and print its return value.
Program:
def digits(n):#defining a method digits that takes n variable in the parameter
x=len(str(n))#defining a variable x that converts parameter value into string and counts its length value
return x#using return keyword that return the length of x
n=int(input("Ennter number: "))#defining a variable n that inputs value
print(n, "has", digits(n) ,"digits") #using print method that calls the digits method and print its value
OR
def digits(n):#defining a method digits that takes n variable in the parameter
x = 0#defining an integer variable x
if n == 0:#defining an if block that checks n value equal to 0
return 1#return value 1
while (n > 0):#defining a while loop that check n value grater than 0
x += 1#incrementing the value of x
n =n//10#calculating the digits
return x#return digits value
n=int(input("Ennter number: "))#defining a variable n that inputs value
print(n, "has", digits(n) ,"digits") #using print method that calls the digits method and print its value
Output:
Please find the attached file.
Learn more:
brainly.com/question/21289326
