Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python varibale“;计数“;韩元';即使if语句(要递增)与条件匹配,也不能递增_Python_Python 3.x - Fatal编程技术网

Python varibale“;计数“;韩元';即使if语句(要递增)与条件匹配,也不能递增

Python varibale“;计数“;韩元';即使if语句(要递增)与条件匹配,也不能递增,python,python-3.x,Python,Python 3.x,我试图使它在包含所有计数变量的列表中选择最大值…您将看到我下面的意思 import string def solve(s): assigned_alphabet = {string.ascii_lowercase[num] : num + 1 for num in range(26)} non_vowels = "".join([char if char not in "aeiou" else " " for char in s]).split() count_list

我试图使它在包含所有计数变量的列表中选择最大值…您将看到我下面的意思

import string

def solve(s):
    assigned_alphabet = {string.ascii_lowercase[num] : num + 1 for num in range(26)}
    non_vowels = "".join([char if char not in "aeiou" else " " for char in s]).split()
    count_list = []
    for item in non_vowels:
        count = 0
        if len(item) > 1:
            for char in item:
                count += assigned_alphabet[char]
            count_list.append(count)
        elif len(item) == 1:
            count += assigned_alphabet[item]
    return max(count_list)
它通过了以下测试:

Test.assert_equals(solve("chruschtschov"),80)
Test.assert_equals(solve("khrushchev"),38)
Test.assert_equals(solve("strength"),57)
Test.assert_equals(solve("catchphrase"),73)
Test.assert_equals(solve("twelfthstreet"),103)
Test.assert_equals(solve("mischtschenkoana"),80)
但出于某种原因,这次失败了:

Test.assert_equals(solve("zodiac"),26)

这可能是一个愚蠢的错误,我的眼睛累了,我就是找不到方法来修复它。

我修复了它!我没有意识到在那之后:

elif len(item) == 1:
    count += assigned_alphabet[item]

我没有把它添加到计数列表中,我不知道你的程序在做什么,但我看到了错误

这一行:

non_vowels = "".join([char if char not in "aeiou" else " " for char in s]).split()
将输入字符串拆分为包含元音之间非元音的字符串列表

''.join([char if char not in "aeiou" else " " for char in 'mischtschenkoana']).split()

--> ['m', 'schtsch', 'nk', 'n']

黄道带的问题是,此列表中的值没有一个长度大于1

['z', 'd', 'c']
对于len=1的情况,代码中的if子句不会更新返回的内容

elif len(item) == 1:
    count += assigned_alphabet[item]
return max(count_list)

您只更新计数,不更新计数列表。所以您返回的是一个空列表。

我不明白,您的程序应该做什么?@Danii基本上它做的是max([一个包含非字母元音列表中每个字符串的计数变量的列表])->例如“strength”,当传递到函数中时,
非字母元音
变量将等于
[“str”,“ngth”]
,,然后,包含
{“a”:1,“b”:2等}
的字典将使
“str”
==57,它与
“length”
做相同的模式,然后选择存储在
计数列表中的两个中的
max()
,我也不明白,你能给代码添加更多文档吗?也许是一个更好的函数解释和一些解释更重要代码行的注释?