美文网首页
python return 翻车记

python return 翻车记

作者: zd200572 | 来源:发表于2017-08-22 16:43 被阅读0次

不是最近在学think Python嘛,失败的是已经完全按照作者答案的算法改写了程序,竟然还是得到的结果不一样。一句一句地分析,花了很长时间,才发现是return语句用错了,放在了程序中间,导致程序提前结束,后边的语句不可能执行的,也是醉了。
def is_pallindrome_number():
list = []
for i in range(198888, 200000):
#print(i)
j = str(i)
if j[::-1][:4] == j[2:]:
#print(i)

            if str(i + 1)[::-1][:5] == str(i + 1)[1:]:
                #print(i)
                if str(i + 2)[::-1][1:5] == str(i + 2)[1:5]:
                    #print(i)
                    
                    if str(i + 3)[::-1] == str(i + 3):
                        #print(i)
                        list.append(i)
                        #这是才开始我的return语句位置return list
    return list 

作者的代码:
from future import print_function, division

def has_palindrome(i, start, length):
"""Checks if the string representation of i has a palindrome.

i: integer
start: where in the string to start
length: length of the palindrome to check for
"""
s = str(i)[start:start+length]
return s[::-1] == s


def check(i):
"""Checks if the integer (i) has the desired properties.

i: int
"""
return (has_palindrome(i, 2, 4) and
has_palindrome(i+1, 1, 5) and
has_palindrome(i+2, 1, 4) and
has_palindrome(i+3, 0, 6))


def check_all():
"""Enumerate the six-digit numbers and print any winners.
"""
i = 100000
while i <= 999996:
if check(i):
print(i)
i = i + 1


print('The following are the possible odometer readings:')
check_all()
print()

相关文章

  • python return 翻车记

    不是最近在学think Python嘛,失败的是已经完全按照作者答案的算法改写了程序,竟然还是得到的结果不一样。一...

  • Python 中return用法及意义

    1.python 中 return用法 The key word "return" which should be...

  • Python: Return

    In the self-defined function, maybe there are many items ...

  • return;debug。

    python def函数 没有return/return后没有参数,则返回None值 return跳出函数窗口,执...

  • python-复盘-yield & return区别

    python-复盘-yield & return区别

  • Python: 浅析 return 和 finally 共同挖的

    初识 return 相信每一个用过Python函数的童鞋, 肯定会用过return语句, return顾名思义, ...

  • 函数

    1.函数返回值 return 后面可以接多个值 return 'abc',[42,'python'],'guido...

  • “翻车”记

    别怕,不是交通事故。 某些日子,注定不能煮菜做饭,比如昨天。一顿午饭,翻了三次车。 新米上市,烧菜饭是好选择。米好...

  • Python: return语句

    环境:Windows 10 64-bit, python == 3.6.4 , PyCharm CE == 201...

  • Python--return

    return:返回 有些小伙伴对return的用法有点蒙逼,小编今天给大家举个例,马上就明白了 大家都知道prin...

网友评论

      本文标题:python return 翻车记

      本文链接:https://www.haomeiwen.com/subject/jlmldxtx.html