我正在做一个python课程,练习之一是编写一个将“ Doctor”添加到名称的函数。说明是:
定义带有参数名称的函数make_doctor() 获取变量full_name的用户输入 使用full_name作为参数调用函数 打印返回值
我的代码是:
def make_doctor(name):
full_name = input("Doctor ")
return full_name
print(make_doctor(full_name))
但是,我不断收到以下错误消息:
NameError Traceback (most recent call last)
<ipython-input-25-da98f29e6ceb> in <module>()
5 return full_name
6
----> 7 print(make_doctor(full_name))
NameError: name 'full_name' is not defined
请问有人可以帮忙吗?
谢谢
答案 0 :(得分:0)
在您的代码中,变量full_name
是函数make_doctor
的局部变量
尝试一下:
def make_doctor(name):
return "Doctor "+name
full_name = input()
print(make_doctor(full_name))
答案 1 :(得分:0)
您的代码有很多问题。
将input
置于函数之外。将输入传递到make_doctor
以向其中添加“医生”,然后打印该文件。
非常重要的旁注:如果是python2,请使用raw_input()
,如果是python3,请使用input()
。不要在python 2中使用input()
,它是一个表达式求值器,而不是字符串。
def make_doctor(name):
return "Doctor {}".format(name)
name = raw_input("Enter your name here!!") # if python2
# name = input("Enter your name here!!") # if python3
print(make_doctor(name=name))