基本上,该功能必须提示用户输入一周中某一天的前两个字母,它应该返回一周中完整的措辞日。它应该继续提示用户执行此操作,直到用户输入无效的两个字符。
def abr():
abr = input("Enter a day abbreviation:")
days = {Su: Sunday, Mo: Monday, Tu: Tuesday, We:Wenesday, Th: Thursday, Fr: Friday, Sa: Saturday, Su: Sunday}
if days in abr:
return days['']
我试图用字典来解决这个问题,但我对字典及其限制不太熟悉。
答案 0 :(得分:3)
kojiro 是对的。你必须扭转你的条件
if abr in days:
return days[abr]
不要忘记使用字符串作为字典键和值,因为用户输入将作为字符串返回,您将用作键。
您必须使用raw_input
input
is actually equivalent to an eval,这需要您的用户输入“Tu”而非Tu。
对于循环,您可以使用while循环并在检测到无效的abreviation时退出
def abr():
abr = raw_input("Enter a day abbreviation:")
days = {"Su": "Sunday",
"Mo": "Monday",
"Tu": "Tuesday",
"We": "Wednesday",
"Th": "Thursday",
"Fr": "Friday",
"Sa": "Saturday",
"Su": "Sunday"}
if abr in days:
return days[abr]
return None
while(1):
day = abr()
if day is not None:
print day
else:
break
答案 1 :(得分:2)
你并没有太远,你只是让订单逆转了。哦,那些字典键和值都应该是字符串。
def abr():
"""Return the day given a two-letter abbreviation."""
# Don't use the function name as a variable in the function.
# It's just not good practice.
abbrev = input("Enter a day abbreviation:")
days = {
"Su": "Sunday",
"Mo": "Monday",
"Tu": "Tuesday",
"We": "Wednesday", # [Spelling fixed]
"Th": "Thursday",
"Fr": "Friday",
"Sa": "Saturday",
} # Extra Sunday removed
return days.get(abbrev, '')
要使循环行为发生所以它一直持续到它们点击无效的缩写,在循环中调用该函数:
while True:
if abr() == "":
break
或者更短一些:
while abr() != "":
pass
答案 2 :(得分:0)
current_designation_upper = Transformed_Data.designation.tolist()[i] .upper() current_designation_upper_array = re.split(“[-.!@#$%^& amp; *()_ + =〜`:<>,/'|} {[] |]”,current_designation_upper) role_current_designation_null_removed = [如果是x,则为current_designation_upper_array中的x的x] current_designation_upper_array_with_abbrevation ='' 对于范围内的k(0,len(role_current_designation_null_removed)): temp_current_designation_upper_array_with_abbrevation = current_designation_upper_array_with_abbrevation + role_current_designation_null_removed [k] [0]