Python Euler项目问题#17-我的代码中的错误

Python Euler项目问题#17-我的代码中的错误,python,Python,所以我现在正在解决第17个问题,长度比实际解大100 问题描述如下: 如果数字1到5是用文字写出来的:一,二,三,四, 五个,那么总共使用了3+3+5+4+4=19个字母 如果1到1000(一千)之间的所有数字都是 用文字写出来,会用多少个字母 注意:不计算空格或连字符。例如,342(三百 和四十二)包含23个字母和115(一百一十五) 包含20个字母。在书写数字时使用“and”是一种常见的用法 符合英国用法 对于我的代码,我有一个: def problem17(max): number

所以我现在正在解决第17个问题,长度比实际解大100

问题描述如下:

如果数字1到5是用文字写出来的:一,二,三,四, 五个,那么总共使用了3+3+5+4+4=19个字母

如果1到1000(一千)之间的所有数字都是 用文字写出来,会用多少个字母

注意:不计算空格或连字符。例如,342(三百 和四十二)包含23个字母和115(一百一十五) 包含20个字母。在书写数字时使用“and”是一种常见的用法 符合英国用法

对于我的代码,我有一个:

def problem17(max):
    numbers = {
        0:  0,
        1 : len('one'),
        2 : len('two'),
        3 : len('three'),
        4 : len('four'),
        5 : len('five'),
        6 : len('six'),
        7 : len('seven'),
        8 : len('eight'),
        9 : len('nine'),
        10: len('ten'),
        11: len('eleven'),
        12: len('twelve'),
        13: len('thirteen'),
        14: len('fourteen'),
        15: len('fifteen'),
        16: len('sixteen'),
        17: len('seventeen'),
        18: len('eighteen'),
        19: len('nineteen')
    }

    number_tenths = {
        2: len('twenty'),
        3: len('thirty'),
        4: len('fourty'),
        5: len('fifty'),
        6: len('sixty'),
        7: len('seventy'),
        8: len('eighty'),
        9: len('ninety')
    }

    hundred = len('hundred')
    thousand = len('thousand')
    andl = len('and')

    i = 1
    length = 0
    under_onehundred = lambda i: numbers[i] if i < 20 else number_tenths[math.floor(i / 10)] + numbers[i % 10]
    hundreds = lambda i: numbers[math.floor(i / 100)] + hundred
    while i <= max:
        if i < 100:
            length += under_onehundred(i)
        elif i % 100 == 0 and i % 1000 != 0:
            length += hundreds(i)
        elif i >= 100 and i < 1000:
            length += hundreds(i) + andl + under_onehundred(i % 100)
        elif i % 1000 == 0:
            length += numbers[math.floor(i / 1000)] + thousand

        i += 1

    return length
def问题17(最大值):
数字={
0:  0,
1:len(“一”),
2:len('two'),
3:len(“三”),
4:len('four'),
5:len(“五”),
6:len('six'),
7:len(“七”),
8:len(“八”),
9:len(“九”),
10:len(“十”),
11:len(“十一”),
12:len(“十二”),
13:len(“十三”),
14:len(“十四”),
15:len(“十五”),
16:len(“十六”),
17:len(“十七”),
18:len(“十八”),
19:len(“十九”)
}
十分之一的数量={
2:len(“二十”),
3:len(“三十”),
4:len('fourty'),
5:len(“五十”),
6:len(“六十”),
7:len(“七十”),
8:len(“八十”),
9:len(“九十”)
}
百=len(‘百’)
千=千(千)
andl=len('and')
i=1
长度=0
在λ100=lambda i下:如果i<20,则为数字[i];如果i<20,则为数字的十分之一[math.floor(i/10)]+数字[i%10]
百分之一百=λi:数字[数学楼层(i/100)]+一百
当i=100且i<1000时:
长度+=100(i)+和L+小于100(i%100)
elif i%1000==0:
长度+=数字[数学楼层(i/1000)]+千
i+=1
返回长度
如果我用问题17(1000)调用它,它将输出
21224
,而不是正确的答案

编辑:我对python还很陌生,所以我觉得总的来说还有一些地方需要改进,请在评论中告诉我


我的问题是,我的代码有什么问题?

我不认为这是您的代码的唯一问题,但这是一个简单的问题:不要在数字上使用
is
is not
;使用
==
=取而代之。您可能会对以下行为感到惊讶:

>1000+2000等于3000
假的
>>> 1000 + 2000 == 3000
真的
说明:
=
操作符测试相等性,但
是测试对象标识。在本例中,有两个不同的
int
对象等于3000,但它们不是同一个对象,因此
is
给出
False
一个问题可能是:

hundreds = lambda n: numbers[math.floor(i / 100)] + hundred
应该是:

hundreds = lambda n: numbers[math.floor(n / 100)] + hundred

另一个问题可能是40的拼写,应该是“四十”而不是“四十”

您是否尝试过使用调试器跟踪它?类似于。我使用了我以前的
打印和死亡
策略,如果我不能访问xdebug,我将在PHP中使用这种策略。你知道我可以在VSCode中使用的一个好工具吗(遗憾的是,在我的工作场所无法访问PyCharm)?例如,IDLE有一个调试器,如果Python是的话,它可能已经安装在你的系统上了。对于调试,修改代码的版本可能是有益的,它实际上将数字打印为文本,然后才确定长度。如果您将数字作为单词,则可以使用以下内容确定长度。numlen=len(数字作为单词。替换(“,”)的范围应该快一点。好的,知道这个很好。我相应地更改了if语句,但它仍然提供相同的输出。很好地发现,尝试了它,但没有改变任何内容:(在一秒钟内,虽然我不确定这是否修复了它,因为它看起来,在您的上下文中I和n总是相同的。您仍然应该修复它,尽管将n更改为I,但输出仍然相同。在OPenhanced中更新了我的代码。请检查40(四十而不是四十)Reference()的拼写。)天哪。是的,这真的是第四十四部分。太尴尬了。非常感谢你