英文:
What is meant by ''set' object is not subscriptable' and how do i remove it from my code
问题
以下是您要翻译的代码部分:
# 定义月份列表
months = {
"January": "1",
"February": "2",
"March": "3",
"April": "4",
"May": "5",
"June": "6",
"July": "7",
"August": "8",
"September": "9",
"October": "10",
"November": "11",
"December": "12"
}
# 输入日期
date_input = input("Date: ")
# 循环
while True:
try:
# 月份---------------------------------------------------------------------------
if "/" in date_input:
month, day, year = date_input.split("/")
month = month
break
elif " " in date_input and "," in date_input:
month, day, year = date_input.split(" ")
day = day.replace(",", "")
break
else:
date_input = input("Date: ")
except ValueError:
pass
while True:
try:
# 月份------------------------------------------------------------------------------
if month.isalpha():
for item in months:
if month == item:
month = (f"{int(months[item]):02}")
break
elif month.isdigit():
month = int(month)
if month > 0 and month < 13:
if month > 0 and month < 10:
month = (f"{int(month):02}")
break
else:
date_input = input("Date: ")
break
except ValueError:
pass
while True:
try:
# 日---------------------------------------------------------------------------
if int(day) > 0 and int(day) < 32:
if int(day) > 0 and int(day) < 10:
day = (f"{int(day):02}")
day = int(day)
break
except ValueError:
pass
print(year, "-", month, "-", day, sep="")
请注意,我已经将代码部分翻译为中文,如您所请求。
英文:
Question: implement a program that prompts the user for a date, anno Domini, in month-day-year order, formatted like 9/8/1636 or September 8, 1636. Then output that same date in YYYY-MM-DD format. If the user’s input is not a valid date in either format, prompt the user again. Assume that every month has no more than 31 days; no need to validate whether a month has 28, 29, 30, or 31 days.
My Solution: Comments are before each part in the code. On line 44, (marked in aesteriks (*)) it shows "TYPEERROR: 'set' is not subscriptable when i input the date : January 12, 1234. I dont know what it means or what I'm doing wrong. What that part is supposed to do is store the month's number from the list and update the variable month
.
Here's my code:
#define list
months = { "January", "1"
"February", "2"
"March","3"
"April","4"
"May","5"
"June","6"
"July","7"
"August","8"
"September","9"
"October","10"
"November","11"
"December""12"
}
#input date
date_input = input("Date: ")
#loop
while True:
try:
#MONTH---------------------------------------------------------------------------
if "/" in date_input:
month, day, year = date_input.split("/")
month = month
break
elif " " in date_input and "," in date_input:
month, day, year = date_input.split(" ")
day = day.replace(",", "")
break
else:
date_input = input("Date: ")
except ValueError:
pass
while True:
try:
#MONTH------------------------------------------------------------------------------
if month.isalpha():
for item in months:
if month == item:
**month = (f"{int(months[item]):02}")**
break
elif month.isdigit():
month = int(month)
if month > 0 and month < 13:
if month > 0 and month < 10:
month = (f"{int(month):02}")
break
else:
date_input = input("Date: ")
break
except ValueError:
pass
while True:
try:
#DAY---------------------------------------------------------------------------
if int(day) > 0 and int(day) < 32:
if int(day) > 0 and int(day) < 10:
day = (f"{int(day):02}")
day = int(day)
break
except ValueError:
pass
print(year, "-", month, "-", day, sep="")
答案1
得分: 1
months = { "January": "1",
"February": "2",
"March": "3",
"April": "4",
"May": "5",
"June": "6",
"July": "7",
"August": "8",
"September": "9",
"October": "10",
"November": "11",
"December": "12"
}
英文:
months = { "January", "1"
"February", "2"
"March","3"
"April","4"
"May","5"
"June","6"
"July","7"
"August","8"
"September","9"
"October","10"
"November","11"
"December""12"
}
is a set
, not dict
. May be you want
months = { "January": "1",
"February": "2",
"March": "3",
"April": "4",
"May": "5",
"June": "6",
"July": "7",
"August": "8",
"September": "9",
"October": "10",
"November": "11",
"December":"12"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论