Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Spy Number

This Python code example illustrates how to determine whether a given number is a “spy number.” A spy number is a number where the sum of its digits is equal to the product of its digits. The code takes an input number, processes its digits, and then checks if it meets the criteria of a spy number.

Code Example 1

def checkSpy(number):
    sum, mul = 0, 1

    while number > 0:
        rem = number % 10
        sum += rem
        mul *= rem
        number = int(number / 10)

    if sum == mul:
        return True
    else:
        return False


number = int(input("Enter a number to check for spy number: "))

if checkSpy(number) == True:
    print(str(number) + " is a spy number")
else:
    print(str(number) + " is not a spy number")

Detailed Code Explanation

Function Definition

checkSpy(number)

The function checkSpy takes a single integer argument, number, and determines whether it is a spy number.

def checkSpy(number):
    sum, mul = 0, 1
  • Initialization: Two variables, sum and mul, are initialized to 0 and 1 respectively. sum will store the sum of the digits, and mul will store the product of the digits.

Processing the Digits

while number > 0:

A while loop is used to process each digit of the number.

while number > 0:
    rem = number % 10
    sum += rem
    mul *= rem
    number = int(number / 10)
  • Remainder Calculation: rem = number % 10 extracts the last digit of number.
  • Sum and Product Update: The extracted digit is added to sum and multiplied with mul.
  • Number Reduction: number = int(number / 10) removes the last digit of number.

Spy Number Check

if sum == mul:

After processing all digits, the function checks if the sum of the digits equals the product of the digits.

if sum == mul:
    return True
else:
    return False
  • Return Value: The function returns True if the number is a spy number and False otherwise.

Main Code

User Input and Function Call

The main code segment takes an integer input from the user and calls the checkSpy function.

number = int(input("Enter a number to check for spy number: "))
  • User Input: The user is prompted to enter a number, which is then converted to an integer.

Output

The result of the checkSpy function is used to print whether the number is a spy number.

if checkSpy(number) == True:
    print(str(number) + " is a spy number")
else:
    print(str(number) + " is not a spy number")
  • Conditional Check: Depending on the function’s return value, an appropriate message is printed.

This Python code example demonstrates how to find and print all spy numbers up to a given upper limit. A spy number is defined as a number where the sum of its digits equals the product of its digits. The script includes a function to check if a number is a spy number and uses a loop to find all such numbers within the specified range. This example highlights the use of loops, conditional statements, and basic arithmetic operations in Python.

Code Example 2

def checkSpy(number):
    sum, mul = 0, 1

    while number > 0:
        rem = number % 10
        sum += rem
        mul *= rem
        number = int(number / 10)

    if sum == mul:
        return True
    else:
        return False


max = int(input("Enter the upper bound to find spy numbers: "))

print("Spy numbers: ")
for i in range(1, max):
    if checkSpy(i):
        print(i, end=" ")

Code Explanation

User Input and Function Call

The main code segment takes an integer input from the user and calls the checkSpy function for each number up to the specified limit.

max = int(input("Enter the upper bound to find spy numbers: "))
  • User Input: The user is prompted to enter the upper bound, which is then converted to an integer and stored in max.

Loop to Find Spy Numbers

for i in range(1, max):

A for loop iterates through each number from 1 to max - 1.

print("Spy numbers: ")
for i in range(1, max):
    if checkSpy(i):
        print(i, end=" ")
  • Range Iteration: The loop iterates over each integer i in the range from 1 to max - 1.
  • Spy Number Check: The checkSpy function is called for each i.
  • Output: If checkSpy(i) returns True, i is printed as a spy number, with numbers separated by spaces.