python - 调用函数进行打印时,我如何让它从我的 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))
解决方案
由于void()
只有调用input()
,您可以完全取消该函数,只需convert_word()
像这样调用:
print(convert_word(input('some prompt >')))
void()
如果您出于某种原因确实需要该功能:
print(convert_word(void()))
如果您愿意,您可以更改声明void()
以删除输入参数,因为它从未使用过。
推荐阅读
- git - 在提交更改时,我经常看到并提交不同的冗余文件。有没有办法来解决这个问题?
- html - 有没有一种方法可以在没有额外 CSS 文件的情况下制作 Textarea 全屏?
- django - Django 根据重复频率每周、每月或每年自动重新发布数据
- .net-core - 如何将json数组信息传递给net core web api
- visual-studio-2019 - 当我在 Visual Studio 中添加一个新项目时,它不会被添加到解决方案文件中
- bash - 将变量名传递给函数以测试存在和非空
- node.js - 无法从自定义域 HTTPS 访问 Elastic Beanstalk(单实例)
- c++ - 无法将 fpatan 汇编器函数中的浮点值返回到 C++ 程序中
- android - Android 为不同的屏幕尺寸调整所有元素的大小
- mongodb - 特定数据库的猫鼬列表集合