如何在python中获得armstrong程序的输出?
我在执行下面的代码时遇到了错误,有人能帮忙吗如何在python中获得armstrong程序的输出?,python,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 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'
输入值1
输入值50
回溯(最近一次呼叫最后一次):
文件“C:\Users\Desktop\TOM\armstrong使用列表和dic.py”,第25行,在
对象计算()
文件“C:\Users\Desktop\TOM\armstrong using list and dic.py”,第16行,在计算中
a=len(str(自我编号))
AttributeError:“armstrong”对象没有属性“number”
正如错误所说,“armstrong”对象没有属性“number”。我不太清楚你想做什么,但是你想用self.number
计算一些东西,但是你从来没有在构造函数中创建过属性(\uuuuuu init\uuu
)。你应该先这样做。阿姆斯特朗有两个属性:self.Lower
和self.Upper
。你为什么试图访问self.number?你从哪里得到的代码<已定义但未使用代码>下部和上部,编号
未定义但已使用