繁体   English   中英

如何通过变量/定义的术语插入多个输入并使用代码获得多个输出? (Python)

[英]How do I insert multiple inputs via a variable/defined term and get multiple outputs with a code? (Python)

我试图让我的基本音节计数器通过多个单词运行,但我不知道如何做到这一点而不会出错。 在下面的代码中,我创建了一个包含两个单词的占位符变量,用于计算音节的代码。 但是,代码最终无法运行并且不会产生任何输出。 有谁知道该怎么做?

new_variable = "WORD", "AGAIN"

def syllable_count(word):
    word = word.lower()
    count = 0
    vowels = "aeiouy"
    if word[0] in vowels:
        count += 1
    for index in range(1, len(word)):
        if word[index] in vowels and word[index - 1] not in vowels:
            count += 1
    if word.endswith("e"):
        count -= 1
    if word.endswith("le"):
        count += 1
    if word.endswith("ia"):
        count += 1
    if count == 0:
        count += 1
    if word.endswith("management"):
        count -= 1
    if word.endswith("announcement"):
        count -= 1
    return count


print(syllable_count(new_variable))

正如评论中提到的@TimRoberts。 您将元组而不是字符串传递给syllable_counter函数。

您需要做的就是遍历元组,即new_varieble ,然后为每个值调用您的syllable_counter

只需删除最后一行并写下这两行:

for word in new_variable:
    print(syllable_counter(word)) 
new_variable = "WORD", "AGAIN"
print(new_variable)

('WORD', 'AGAIN')

您可以尝试遍历 new_variable列表,例如:

for new_variable in ["WORD", "AGAIN"]:
    print(syllable_count(new_variable))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM