Numerical Digits - Python Programming Exercise

This Python program demonstrates how to calculate the number of digits in a positive integer by repeatedly dividing the number by 10. If the user enters a negative integer, the program will display a warning message and proceed to calculate the number of digits for the equivalent positive integer. This solution is a great way to practice conditional statements and loops in Python, as well as handling user input validation effectively. With this program, you'll learn to handle both positive and negative integers and extract key information efficiently. In this Python exercise, the program prompts the user to input a number, then calculates how many digits the number has by performing a series of divisions. The program will also manage edge cases, such as handling negative numbers, by converting them to their positive equivalent and informing the user of the input's sign. This simple yet powerful task enhances your understanding of integer manipulation and error handling in Python, making it an essential addition to any beginner’s learning journey.

 Category

Mastering Flow Control

 Exercise

Numerical Digits

 Objective

Develop a Python program to calculate the number of digits in a positive integer (hint: this can be done by repeatedly dividing by 10). If the user enters a negative integer, the program should display a warning message and proceed to calculate the number of digits for the equivalent positive integer.

For example:
Number = 32
2 digits
Number = -4000
(Warning: it is a negative number) 4 digits

 Example Python Exercise

 Copy Python Code
# Prompt the user to enter a number
num = int(input("Please enter a number: "))

# Check if the number is negative
if num < 0:
    print("Warning: it is a negative number")
    num = abs(num)  # Convert the number to its positive equivalent

# Initialize the digit counter
digits = 0

# Use a while loop to count the number of digits
while num > 0:
    num //= 10
    digits += 1

# Display the number of digits
print(f"{digits} digits")

 Output

Please enter a number: 32
2 digits

Please enter a number: -4000
Warning: it is a negative number
4 digits

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Empty Square

    This Python program prompts the user to input a symbol and a width, then displays a hollow square with the specified width. The square's outer border is...

  •  Output

    This Python program prompts the user for two integer numbers and calculates their product without using the "*" operator. Instead, it uses consecutiv...

  •  Absolute Magnitude

    This Python program calculates and displays the absolute value of a number x. The absolute value of a number is defined as the number itself if it is positive,...

  •  Empty Rectangle

    This Python program prompts the user for a symbol, width, and height, and then displays a hollow rectangle using that symbol for the outer border...

  •  Data Analysis

    This Python program calculates various basic statistical operations such as sum, average, minimum, and maximum based on user input. The pr...

  •  Ternary Operator, Positive & Lesser

    This Python program prompts the user to input two numbers and then uses the conditional operator (?) to perform a series of checks on the input. The program first che...