What is meant by ''set' object is not subscriptable' and how do i remove it from my code

huangapple go评论116阅读模式
英文:

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 = { &quot;January&quot;, &quot;1&quot;
&quot;February&quot;, &quot;2&quot;
&quot;March&quot;,&quot;3&quot;
&quot;April&quot;,&quot;4&quot;
&quot;May&quot;,&quot;5&quot;
&quot;June&quot;,&quot;6&quot;
&quot;July&quot;,&quot;7&quot;
&quot;August&quot;,&quot;8&quot;
&quot;September&quot;,&quot;9&quot;
&quot;October&quot;,&quot;10&quot;
&quot;November&quot;,&quot;11&quot;
&quot;December&quot;&quot;12&quot;
}
#input date
date_input = input(&quot;Date: &quot;)
#loop
while True:
try:
#MONTH---------------------------------------------------------------------------
if &quot;/&quot; in date_input:
month, day, year = date_input.split(&quot;/&quot;)
month = month
break
elif &quot; &quot; in date_input and &quot;,&quot; in date_input:
month, day, year = date_input.split(&quot; &quot;)
day = day.replace(&quot;,&quot;, &quot;&quot;)
break
else:
date_input = input(&quot;Date: &quot;)
except ValueError:
pass
while True:
try:
#MONTH------------------------------------------------------------------------------
if month.isalpha():
for item in months:
if month == item:
**month = (f&quot;{int(months[item]):02}&quot;)**
break
elif month.isdigit():
month = int(month)
if month &gt; 0 and month &lt; 13:
if month &gt; 0 and month &lt; 10:
month = (f&quot;{int(month):02}&quot;)
break
else:
date_input = input(&quot;Date: &quot;)
break
except ValueError:
pass
while True:
try:
#DAY---------------------------------------------------------------------------
if int(day) &gt; 0 and int(day) &lt; 32:
if int(day) &gt; 0 and int(day) &lt; 10:
day = (f&quot;{int(day):02}&quot;)
day = int(day)
break
except ValueError:
pass
print(year, &quot;-&quot;, month, &quot;-&quot;, day, sep=&quot;&quot;)

答案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 = { &quot;January&quot;, &quot;1&quot;
&quot;February&quot;, &quot;2&quot;
&quot;March&quot;,&quot;3&quot;
&quot;April&quot;,&quot;4&quot;
&quot;May&quot;,&quot;5&quot;
&quot;June&quot;,&quot;6&quot;
&quot;July&quot;,&quot;7&quot;
&quot;August&quot;,&quot;8&quot;
&quot;September&quot;,&quot;9&quot;
&quot;October&quot;,&quot;10&quot;
&quot;November&quot;,&quot;11&quot;
&quot;December&quot;&quot;12&quot;
}

is a set, not dict. May be you want

months = { &quot;January&quot;: &quot;1&quot;,
&quot;February&quot;: &quot;2&quot;,
&quot;March&quot;: &quot;3&quot;,
&quot;April&quot;: &quot;4&quot;,
&quot;May&quot;: &quot;5&quot;,
&quot;June&quot;: &quot;6&quot;,
&quot;July&quot;: &quot;7&quot;,
&quot;August&quot;: &quot;8&quot;,
&quot;September&quot;: &quot;9&quot;,
&quot;October&quot;: &quot;10&quot;,
&quot;November&quot;: &quot;11&quot;,
&quot;December&quot;:&quot;12&quot;
}

huangapple
  • 本文由 发表于 2023年6月13日 10:28:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定