-2

我需要检查一个数字是否是 Lychrel 数字,如果不打印循环直到它到达回文的次数,由于某种原因它总是打印 0

    num = input('please enter your number ')
num = int(num)
count = 0
for i in range(1, 500):
    if str(num) == str(num)[::-1]:
        print(count)
        exit()
    else:
        num = num + int(str(num)[::-1])
        count += 1
print('True')

更新:感谢 Vova,代码现在可以正常工作。固定代码:

    num = int(input('please enter your number '))
count = 0
for i in range(1, 500):
    if str(num) == str(num)[::-1]:
        print(count)
        exit()
    else:
        num = num + int(str(num)[::-1])
        count += 1
print('True')
4

1 回答 1

0

每次输入 1 个数字(0..9)时,它将打印 0

当您输入超过 1 个数字而不是镜像数字时,例如 2、3、4 等(10,234 等),它将打印 1

此外,它永远不会打印 True,因为你在 if 语句中退出()它

尝试输入 23,它会打印 1:

num = int(input('please enter your number '))
count = 0
for i in range(1, 500):
    if str(num) == str(num)[::-1]:
        print(count)
        exit()
    else:
        num = num + int(str(num)[::-1])
        count += 1
print('True')
于 2021-03-22T12:57:10.797 回答