仅供参考,我非常喜欢Python和编码,我几天前下载了它,每天练习一小时左右,我在Kindle上购买了一本教程。
现在问题,我正在尝试这个练习,我从用户那里获得输入,然后将其填充到我创建的故事中。像一个疯狂的lib类型的东西。这是代码,下面是我得到的错误。
print('It was a (adjective) October day. The infamous (noun) was (verb) South to escape the Winter.')
def get_adjective():
"""getting the adjective"""
adj=input('Please provide an adjective:')
return adj
def get_noun():
"""getting the noun"""
noun=input('Please provide a noun:')
return noun
def get_verb():
"""getting the verb"""
verb=input('Please provide a verb:')
return verb
get_adjective()
get_noun()
get_verb()
def putting_together(adj,noun,verb):
"""executing story"""
print('It was a {} October day. The infamous {} was {} South to escape the Winter.'.format(adj,noun,verb))
putting_together(adj,noun,verb)
你们中的一些人可能已经知道出了什么问题,这里的任何一种方式都是我在运行时得到的回复
It was a (adjective) October day. The infamous (noun) was (verb) South to escape the Winter.
Please provide an adjective:cold
Please provide a noun:John
Please provide a verb:riding
Traceback (most recent call last):
File "C:/Python36-32/practice.py", line 24, in <module>
putting_together(adj,noun,verb)
NameError: name 'adj' is not defined
这就是它。任何帮助表示赞赏。我会说,我是新手,并不完全熟悉这种语言。麻烦事情会有所帮助。谢谢你。
答案 0 :(得分:1)
你的功能是返回东西,使用它们:
adj = get_adjective()
noun = get_noun()
verb = get_verb()
答案 1 :(得分:1)
问题在于,您要分配从get_adjective()
,get_noun()
和get_verb()
这些路径获得的值。你可以通过以下方式解决它:
putting_together(get_adjective(), get_noun(), get_verb())
或者您可以将每个值分配给变量,正如@bernie在他的回答中所说:
adjective = get_adjective()
noun = get_noun()
verb = get_verb()
putting_together(adjective, noun, verb)