简体   繁体   English

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

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

I'm trying to make my basic syllable counter run through multiple words, but I do not know how to do so without error.我试图让我的基本音节计数器通过多个单词运行,但我不知道如何做到这一点而不会出错。 In the code below, I made a placeholder variable containing two words for the code to count syllables of.在下面的代码中,我创建了一个包含两个单词的占位符变量,用于计算音节的代码。 However, the code ends up not functioning and creates no output.但是,代码最终无法运行并且不会产生任何输出。 Anyone know what to do?有谁知道该怎么做?

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))

As @TimRoberts mentioned in the comments.正如评论中提到的@TimRoberts。 You are passing a tuple instead of a string to your syllable_counter function.您将元组而不是字符串传递给syllable_counter函数。

All you need to do is iterate through the tuple ie new_varieble and then call your syllable_counter for each value.您需要做的就是遍历元组,即new_varieble ,然后为每个值调用您的syllable_counter

Just remove your last line and write these two:只需删除最后一行并写下这两行:

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

('WORD', 'AGAIN')

You could try to iterate through the list of new_variable instead, for example:您可以尝试遍历 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