【发布时间】:2016-09-07 22:34:53
【问题描述】:
我正在尝试制作一个可以找到基思数字的 python 程序。如果你不知道基思数字是什么,这里有一个解释它们的链接: Keith Numbers - Wolfram MathWorld
我的代码是
from decimal import Decimal
from time import sleep
activator1 = 1
while (activator1 == 1):
try:
limit = int(raw_input("How many digits do you want me to stop at?"))
activator1 = 0
except ValueError:
print "You did not enter an integer"
limitlist = []
activator2 = 1
while (activator2 <= limit):
limitlist.append(activator2)
activator2 += 1
print limitlist
add1 = 0
add = 0
count = 9
while 1:
sleep (0.1)
numbers = list(str(count))
for i in limitlist:
if (i > 0) & (add < count):
add = sum(Decimal(i) for i in numbers)
lastnumber = int(numbers[-1])
add1 = lastnumber+int(add)
numbers.reverse()
numbers.pop()
numbers.append(add1)
print add1
print add
print count
print numbers
if (add1 == count):
print"________________________________"
print add1
print count
elif (i > 0) & (add > count):
count += 1
break
它不输出任何错误,但它只是输出
18
9
9
[18]
有人能告诉我为什么它不只是在整数范围内重复找到基思数吗?
【问题讨论】:
标签: python python-2.7 math numbers