如何解决如何解决def函数运行问题
我正在尝试完成一个开始的python类的分配,该类要求我们编写一个def函数,该函数接受字符串作为参数并返回对该词的复数形式的最佳猜测。我已经写出了代码,但是当我尝试运行它时,它会询问我的输入,但是无论我键入什么内容,都将返回PS C:\Users\OneDrive\Documents>
,这是文件在计算机上保存的位置。它没有返回任何语法错误,所以我是否缺少会触发def函数的内容?请帮助我了解我所缺少的内容。
singular_word = input("Please enter a word to be pluralized ")
def pluralize_word(singular_word):
if singular_word[-1] == "x" or "s" or "z":
es_ending = singular_word[:] + "es"
print(es_ending)
return True
elif singular_word[-2:] == "ch":
ch_es_ending = singular_word[:-2] + "es"
print(ch_es_ending)
return
elif singular_word[-1] == "y":
ies_ending = singular_word[:-1] + "ies"
print(ies_ending)
return
elif singular_word[-1] == "o":
oes_ending = singular_word[:] + "es"
print(oes_ending)
return
elif singular_word[-1] == "f":
ves_ending = singular_word[:-1] + "ves"
print(ves_ending)
return
elif singular_word[-2:] == "fe":
fe_ves_ending = singular_word[:-2] + "ves"
print(fe_ves_ending)
return
else:
print(singular_word[:] + "s")
return
解决方法
您定义了一个名为pluralize_word
的函数,但从未调用它!您需要类似
pluralize_word(singular_word)
这需要放在函数定义之后。
还有第二个问题,它是以下内容:
singular_word[-1] == "x" or "s" or "z"
以上内容检查singular_word[-1] == "x"
是否为真。如果不是,则检查"s"
是否为true。 这与singular_word[-1] == "s"
不同! "s"
始终是正确的,因此您永远不会超越第一种情况(总是添加es
)。
你想要
singular_word[-1] == "x" or singular_word[-1] == "s" or singular_word[-1] == "z"
或者,如@John Gordon所建议的,在这种情况下,您可以使用以下简短检查:
singular_word[-1] in "xsz"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。