简体   繁体   English

调用函数进行打印时,如何从我的void函数中获取用户输入?

[英]when calling a function to print, how do i get it to get user input from my void function?

I have a code that will turn a word into pig latin, I'm getting user input from a function. 我有一个可以将单词变成拉丁文的代码,我从一个函数中获取用户输入。 What do i need to put into the 我需要放入什么

print(convert_word(n))

to make it print with user input? 使它与用户输入一起打印?

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

Since void() only calls input() , you could do away with that function entirely and just call convert_word() like this: 由于void()仅调用input() ,因此您可以完全取消该函数,而只需像这样调用convert_word()

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

If you really do need the void() function for some reason: 如果由于某些原因确实需要void()函数:

print(convert_word(void()))

If you like, you can change the declaration of void() to remove the input argument, as it is never used. 如果愿意,您可以更改void()的声明以删除输入参数,因为它从未使用过。

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

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