This Python program reads a four-digit number from the user and counts how many digits in it are zero.
The input must be exactly four digits, and the program only counts the digit 0.
# Read input from user
num = input("Enter a four-digit number: ")
# Validate input
if len(num) == 4 and num.isdigit():
zero_count = num.count('0')
print(f"Number of zero digits: {zero_count}")
else:
print("Input must be a four-digit number containing only digits.")
Input: 1020
Number of zero digits: 2
Input: 1234
Number of zero digits: 0
- The number is read as a string to allow digit-by-digit inspection
- The .count('0') method counts how many times the digit zero appears
- If the input is not exactly four digits or contains non-digit characters, an error message is shown