如何在python中获得armstrong程序的输出?

2024-06-01 01:00:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在执行下面的代码时遇到了错误,有人能帮忙吗

def getting_input():
    while True:
        try:
            x=int(input("enter the value"))
            return x
        except Exception as e:
            print(f'error: {e}')
            continue

class armstrong:
    def __init__(self):
        self.Lower = getting_input()
        self.Upper = getting_input()
    def calculation(self):
            res=0
            a=len(str(self.number))
            temp= self.number
            while temp > 0:
                digit = temp % 10
                res += digit ** a
                temp //= 10
            if self.number == res:
                print(self.number)
obj=armstrong()
obj.calculation()

输出:

enter the value1
enter the value50
Traceback (most recent call last):
  File "C:\Users\Desktop\TOM\armstrong using list and dic.py", line 25, in <module>
    obj.calculation()
  File "C:\Users\Desktop\TOM\armstrong using list and dic.py", line 16, in calculation
    a=len(str(self.number))
AttributeError: 'armstrong' object has no attribute 'number'

Tags: theselfobjnumberinputlendefres