【发布时间】:2018-09-02 15:44:28
【问题描述】:
我正在尝试编写一个函数来询问姓名、姓氏和出生年份。此外,它稍后会打印出姓名首字母和年龄。
首先,它不要求任何东西。
其次,不管我输入什么都会打印错误:
NameError: name "____" is not defined.
我正在使用 Python 3。
代码如下:
def userinfo():
name_ = input("Enter your first name: ")
last_ = input("Enter your last name: ")
year_ = input("Enter your year: ")
initials_ = name_[0] + last_[0]
age_ = (2018 - year_)
_info = ("Your initials are ") + (initials_) + (" and you are ") + (str(age_)) + (" years old.")
if (len(name_) > 0 and len(last_) > 0 and len(year_) > 0 and name_.isalpha() and last_.isalpha()):
return (_info)
else:
return ("Error")
【问题讨论】:
-
这看起来不像完整的代码。请提供minimal reproducible example,以便我们诊断您的问题。谢谢。
-
函数怎么调用?
-
欢迎来到 Stack Overflow。为了帮助您,请提供一个完整的代码示例来重现您所询问的错误。到目前为止,您给出的代码不可能导致该错误。
-
它不要求任何它大概是因为你刚刚定义了函数并没有调用它。为此,只需将
userinfo()放在函数定义外部函数。 -
通过运行此代码,我尝试输入例如“Marina”,我得到了那个错误
标签: python input python-2.x