首页 > 解决方案 > 调用函数进行打印时,我如何让它从我的 void 函数中获取用户输入?

问题描述

我有一个代码可以将一个单词变成拉丁语,我正在从一个函数中获取用户输入。我需要放入什么

print(convert_word(n))

让它与用户输入一起打印?

def void(n):
n = input("Enter the word you want converted to Pig Latin: ")
return n


VOWELS = ('a', 'e', 'i', 'o', 'u')

# Function definition

def convert_word(word):


# Assign the first letter of word to variable first_letter
first_letter = word[0]

# Check if the word starts with a vowel
if first_letter in VOWELS:

    # If it is a vowel, then keep the word as it is and add "hay" to the end
    return word + "hay"

# If the word does not start with a vowel
else:
        # Returns the word except word[0] and add "ay" at the end of the string
    return word[1:] + word[0] + "ay"


# Prompt the user to enter the input string


# Call the function to convert the word to pigLatin
print(convert_word(n))

标签: pythonfunction

解决方案


由于void()只有调用input(),您可以完全取消该函数,只需convert_word()像这样调用:

print(convert_word(input('some prompt >')))

void()如果您出于某种原因确实需要该功能:

print(convert_word(void()))

如果您愿意,您可以更改声明void()以删除输入参数,因为它从未使用过。


推荐阅读