我希望就与此功能相关的事项提出建议。
我已经尝试过对代码进行各种编辑和缩进,但是显示的结果相同。
它显示了NameError: name 'sentence' is not defined
,尽管我已经在函数中定义了它。
代码是:
def about (name, age, likes):
sentence = "Meet {}, he is {} years old and likes {}".format(name,age,likes)
return sentence
about ("Jack", 23, "programming")
print (sentence)
答案 0 :(得分:3)
您应该调用函数并将其分配给变量:
def about(name, age, likes):
sentence = "Meet {}, he is {} years old and likes {}".format(name,age,likes)
return sentence
然后
val = about("Jack", 23, "programming")
print(val)
您也可以使用sentence
代替val
,但这在功能范围上不会是sentence
。
答案 1 :(得分:1)
立即尝试。...
def about (name, age, likes):
sentence = "Meet {}, he is {} years old and likes {}".format(name,age,likes)
return sentence
print(about('rohit',23,'programming'))
sentense
范围已绑定到about
函数...并且尝试将其打印出该函数范围可能不起作用。