这将返回NONE。我错误地嵌套了吗?

时间:2013-06-25 10:00:50

标签: python

behavioural_level定义为raw_input 1,2或3 这里发生了什么?

def summary_behaviour(x):
    if behaviour_level == 1:
        x = random.randint(0,1)
        if x == 0:
            return " "+str(first_name)+" has a rather "+str(shy[random.randint(0,(len(shy)-1))])+" personality. This has certain advantages. One is that "+str(he_she(gender))+" doesn't easily "+str(lose_focus[random.randint(0,(len(lose_focus)-1))])+": however, "+str(he_she(gender))+" can be a litte quiet at times." 
        else:
            if x == 1:
                return " Because of "+str(first_name)+"'s "+str(shy[random.randint(0,len(shy)-1)])+" character "+str(he_she(gender))+" can be a little quiet sometimes. On the plus side, however, "+str(he_she(gender))+" doesn't "+str(lose_focus[random.randint(0,len(lose_focus)-1)])+" very easily."
    elif behaviour_level == 2:
        x = random.randint(2,3)
        if x == 2:
            return str(first_name)+" is usually quite "+str(loud[random.randint(0,(len(loud)-1))])+" in class, and this has advantages and disadvantages. "+str(first_name)+" loves to involved and enjoys speaking, but sometimes "+str(hes_shes(gender))+" too "+str(loud[random.randint(0,(len(loud)-1))])+" to fully concentrate. This is common though. "
        else:
            if x == 3:
                return " Because of "+str(first_name)+"'s "+str(loud[random.randint(0,len(loud)-1)])+" character "+str(he_she(gender))+" is "+str(adjective(int(science_level)))+" at speaking up and volunteering. Occasionally, "+str(his_her(gender))+" "+ str(loud[random.randint(0,len(loud)-1)])+ " nature can cuase "+str(him_her(gender))+ " to "+str(lose_focus[random.randint(0,len(lose_focus)-1)])+" but, that's fairly normal at "+str(his_her(gender))+" age."
    else:
        if behaviour_level == 3:
            x = random.randint(4,5)
            if x == 4:
                return " I would descirbe "+str(fisrt_name)+" as a "+(str(well_mannered[random.randint(0,len(well-mannered)-1)]))+" child who is easy to teach. Rarely is "+str(he_she(gender))+" too "+str(loud[random.randint(0,(len(loud)-1))])+" to focus or too "+str(shy[random.randint(0,(len(shy)-1))])+" too speak up."
            else:
                if x == 5: 
                    return str(first_name)+" a "+ (str(well_ma

2 个答案:

答案 0 :(得分:4)

你的巢很好。

raw_input()返回一个字符串。它看起来并没有将它转换为整数,所以你的函数会进入if / else语句的第一个else:位,因为behaviour_level != 1(但它等于"1"

接下来,if behaviour_level == 3:。由于输入是字符串而不是整数,因此为not True。由于没有else,因此该函数默认返回None

要解决此问题,您可以使用int()函数将字符串转换为整数。

答案 1 :(得分:1)

你可以使用
int(behaviour_level) == 1,2 or 3
比较
作为1 2和3是整数,raw_input将返回一个字符串。


此外,而不是做

else:
   if condition:
     """statements"""

您可以简单地使用:

elif condition:
    """statements"""