#!/usr/bin/python3 # ==================================================================== # test if an integer a an Armstrong number # ==================================================================== import user_interface as ui definition = '''\ An Armstrong number is a special kind of number in math. It is equal the sum of its digits, each raised to the power of the number of digits in the number. For example, there are three digits in the number 153. It is an Armstrong number because 1^3 + 5^3 + 3^3 equals 153.''' print(definition) while True: # ---- ask the user for an integer print() s = ui.get_user_input('Enter a positive integer: ') if not s: break # ---- is what they entered an integer? tf,i = ui.is_integer(s) if not tf: print() print('That was not an integer. Try again.') continue if i < 0: print() print('positive integers only') continue # ---- split what the user's input into a list # ---- what is the length of the list? lst = list(s) l = len(lst) # ---- calculate using the armstrong number algorithem ii = 0 for c in lst: ii += int(c)**l # ---- is the user's input an amrstrong number? print() print(f'ii={ii} i={i} lst={lst}') print() if ii == i: print(f'{s} is an armstrong number') else: print(f'{s} is not an armstrong number')