为什么这个IF / ELIF / ELSE不起作用 - Python?

时间:2017-08-17 15:04:30

标签: python

我不知道为什么这个IF / ELIF / ELSE没有在python中通过,“state_initials”存在并且其中有一个与IF / ELIF / ELSE相对应的变量,但它仍然没有经过。

我做错了什么?

我已经完成了我的问题,但是堆栈溢出不会让我发布,因为它主要是编码所以请忽略它。

job_state = ""

if state_initial == "AL":
    job_state = "Alabama"
elif state_initial == "AK":
    job_state == "Alaska"
elif state_initial == "AS":
    job_state == "American Samoa"
elif state_initial == "AZ":
    job_state == "Arizona"
elif state_initial == "AR":
    job_state == "Arkansas"
elif state_initial == "CA":
    job_state == "California"
elif state_initial == "CO":
    job_state == "Colorado"
elif state_initial == "CT":
    job_state == "Connecticut"
elif state_initial == "DE":
    job_state == "Delaware"
elif state_initial == "DC":
    job_state == "District of Columbia"
elif state_initial == "FL":
    job_state == "Florida"
elif state_initial == "GA":
    job_state == "Georgia"
elif state_initial == "GU":
    job_state == "Guam"
elif state_initial == "HI":
    job_state == "Hawaii"
elif state_initial == "ID":
    job_state == "Idaho"
elif state_initial == "IL":
    job_state == "Illinois"
elif state_initial == "IN":
    job_state == "Indiana"
elif state_initial == "IA":
    job_state == "Iowa"
elif state_initial == "KS":
    job_state == "Kansas"
elif state_initial == "KY":
    job_state == "Kentucky"
elif state_initial == "LA":
    job_state == "Louisiana"
elif state_initial == "ME":
    job_state == "Maine"
elif state_initial == "MD":
    job_state == "Maryland"
elif state_initial == "MH":
    job_state == "Marshall Islands"
elif state_initial == "MA":
    job_state == "Massachusetts"
elif state_initial == "MI":
    job_state == "Michigan"
elif state_initial == "FM":
    job_state == "Micronesia"
elif state_initial == "MN":
    job_state == "Minnesota"
elif state_initial == "MS":
    job_state == "Mississippi"
elif state_initial == "MO":
    job_state == "Missouri"
elif state_initial == "MT":
    job_state == "Montana"
elif state_initial == "NE":
    job_state == "Nebraska"
elif state_initial == "NV":
    job_state == "Nevada"
elif state_initial == "NH":
    job_state == "New Hampshire"
elif state_initial == "NJ":
    job_state == "New Jersey"
elif state_initial == "NM":
    job_state == "New Mexico"
elif state_initial == "NY":
    job_state == "New York"
elif state_initial == "NC":
    job_state == "North Carolina"
elif state_initial == "ND":
    job_state == "North Dakota"
elif state_initial == "OH":
    job_state == "Ohio"
elif state_initial == "OK":
    job_state == "Oklahoma"
elif state_initial == "OR":
    job_state == "Oregon"
elif state_initial == "PW":
    job_state == "Palau"
elif state_initial == "PA":
    job_state == "Pennsylvania"
elif state_initial == "PR":
    job_state == "Puerto Rico"
elif state_initial == "RI":
    job_state == "Rhode Island"
elif state_initial == "SC":
    job_state == "South Carolina"
elif state_initial == "SD":
    job_state == "South Dakota"
elif state_initial == "TN":
    job_state == "Tennessee"
elif state_initial == "TX":
    job_state == "Texas"
elif state_initial == "UT":
    job_state == "Utah"
elif state_initial == "VT":
    job_state == "Vermont"
elif state_initial == "VA":
    job_state == "Virginia"
elif state_initial == "VI":
    job_state == "Virgin Islands"
elif state_initial == "WA":
    job_state == "Washington"
elif state_initial == "WV":
    job_state == "West Virginia"
elif state_initial == "WI":
    job_state == "Wisconsin"
elif state_initial == "WY":
    job_state == "Wyoming"
else:
    job_state = "Unknown_Location"

2 个答案:

答案 0 :(得分:8)

你应该通过对答案做出决定来清理你的代码:

a = [11,11,11,23,37,53,37,37,2,11,23,21,17,12,17,17,17,17,19,19,11,11,33,33]

def is_prime(n):
    from math import sqrt
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(sqrt(n)) + 1, 2))

counter = {k: a.count(k) for k in set(a)}

from operator import itemgetter
while True:
    maximum = max(counter.items(), key=itemgetter(1))
    if is_prime(maximum[1]):
        break
    else:
        counter.pop(maximum[0])
print(maximum)     # (17, 5)
print(maximum[0])  # 17

答案 1 :(得分:8)

关于为什么部分,你没有分配。您正在if - elif语句中检查是否相同。

elif state_initial == "ID":
    job_state == "Idaho" 
              ^^ you are using double here as well.

关于解决方案,您确实应该使用Cory's answer中所述的字典而不是那些。