How do you write the code in Python to determine if a number is a narcissistic number?

A narcissistic number is defined as an n-digit number (n ≥ 3) for which the sum of the nth powers of its digits equals the number itself. For example, 153 is a narcissistic number because 1^3 + 5^3 + 3^3 equals 153.

Here is the Python code to determine if a number is an Armstrong number:

def is_armstrong_number(num):
    # 将数字转化为字符串以获取位数
    str_num = str(num)
    # 获取数字的位数
    n = len(str_num)
    # 计算每个位上数字的 n 次幂之和
    armstrong_sum = sum([int(digit)**n for digit in str_num])
    # 判断是否为水仙花数
    if armstrong_sum == num:
        return True
    else:
        return False

# 调用函数进行测试
num = 153
if is_armstrong_number(num):
    print(num, "是水仙花数")
else:
    print(num, "不是水仙花数")

Output:

153 是水仙花数
bannerAds