-4

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
4

2 回答 2

4

你的窝很好。

raw_input()返回一个字符串。看起来您没有将其转换为整数,因此您的函数进入else:if/else 语句的第一位,因为behaviour_level != 1(而是等于"1"

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

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

于 2013-06-25T10:02:36.783 回答
1

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


此外,而不是做

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

您可以简单地使用:

elif condition:
    """statements"""
于 2013-06-25T10:05:02.907 回答