我正在尝试编写一个要求输入姓名,姓氏和出生年份的函数。另外,稍后还会打印出姓名缩写和年龄。
首先,它不需要任何东西。
第二,不管我输入什么,它都会显示错误:
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")
答案 0 :(得分:0)
这似乎是从dat[] <- lapply(dat, factor, levels = 1:2, labels = c("Yes", "No"))
的声明中抛出的。
检查主要功能是使用双下划线(__)还是三下划线(___)。
正确的synax是main()
希望这会有所帮助!干杯!
答案 1 :(得分:0)
以下是经过更正的经过纠正的代码:
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.'