Why is Python 2 input() giving me 'NameError: name "____" is not defined'?
Why is Python 2 input() giving me 'NameError: name "____" is not defined'?
我正在尝试编写一个函数来询问姓名、姓氏和出生年份。此外,它稍后会打印出姓名首字母和年龄。
首先,它不要求任何东西。
其次,无论我输入什么都将打印错误:
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")
这似乎是从 main()
的声明中抛出的。
检查 main 函数是否使用了双下划线 ( __ ) 或三下划线 ( ___ )。
正确的语法是if __name__ == '__main__':
希望对您有所帮助!干杯!
这是经过一些更正的更正代码:
1) 将 input
替换为 raw_input
(假设您使用的是 python 2.***
)。如果您使用的是版本 3+
,则将 raw_input
替换为 input
。
2) 在计算年龄时将 year_
替换为 int(year_)
,因为用户输入的类型是 str
。
def userinfo():
name_ = raw_input("Enter your first name: ")
last_ = raw_input("Enter your last name: ")
year_ = raw_input("Enter your year: ")
print (name_)
initials_ = name_[0] + last_[0]
age_ = (2018 - int(year_)) # Correction here
_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")
userinfo()
输出
Enter your first name: Donald
Enter your last name: Trump
Enter your year: 1950
Donald
'Your initials are DT and you are 68 years old.'
我正在尝试编写一个函数来询问姓名、姓氏和出生年份。此外,它稍后会打印出姓名首字母和年龄。
首先,它不要求任何东西。
其次,无论我输入什么都将打印错误:
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")
这似乎是从 main()
的声明中抛出的。
检查 main 函数是否使用了双下划线 ( __ ) 或三下划线 ( ___ )。
正确的语法是if __name__ == '__main__':
希望对您有所帮助!干杯!
这是经过一些更正的更正代码:
1) 将 input
替换为 raw_input
(假设您使用的是 python 2.***
)。如果您使用的是版本 3+
,则将 raw_input
替换为 input
。
2) 在计算年龄时将 year_
替换为 int(year_)
,因为用户输入的类型是 str
。
def userinfo():
name_ = raw_input("Enter your first name: ")
last_ = raw_input("Enter your last name: ")
year_ = raw_input("Enter your year: ")
print (name_)
initials_ = name_[0] + last_[0]
age_ = (2018 - int(year_)) # Correction here
_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")
userinfo()
输出
Enter your first name: Donald
Enter your last name: Trump
Enter your year: 1950
Donald
'Your initials are DT and you are 68 years old.'