其他声明不适用于我的if语句

时间:2016-11-14 14:47:26

标签: python if-statement

sentence= ("ask not what your country can do for you ask what you can do for your country")
keyword= input("Input a keyword from the sentence")
words = sentence.split(' ')

for i, word in enumerate(words):
    if keyword == word:
        print(i+1)
    else:
        print("the word,",keyword,",is not in the sentence")

如果没有else语句,将会打印出该单词在句子中的位置,但是,一旦我添加了else语句,即使该单词出现在句子中,#34;单词," ,关键字",不在句子中#34;打印出来。

3 个答案:

答案 0 :(得分:0)

/** * Strips any potential XSS threats out of the value * * @param value * @return */ public static String stripXSS( String value ) { return stripXSS( value, Whitelist.none() ); } /** * Strips any potential XSS threats out of the value excluding * the white listed HTML * * @param value * @param whitelist * @return */ public static String stripXSS( String value, Whitelist whitelist ) { if( StringUtils.isBlank( value ) ) return value; // Use the ESAPI library to avoid encoded attacks. value = ESAPI.encoder().canonicalize( value ); // Avoid null characters value = value.replaceAll("\0", ""); // Clean out HTML Document.OutputSettings outputSettings = new Document.OutputSettings(); outputSettings.escapeMode( EscapeMode.xhtml ); outputSettings.prettyPrint( false ); value = Jsoup.clean( value, "", whitelist, outputSettings ); return value; } 会为每个不是关键字的单词启动,导致许多“失败”。

else条件下使用for-else循环,因此如果没有字词与关键字匹配,您的程序将显示失败的输出

break

答案 1 :(得分:0)

由于else语句在for循环中,它将在循环中显示许多重复的语句,因此你的else语句应该在循环之外缩进,如下所示:

sentence= ("ask not what your country can do for you ask what you can do for 
your country")
keyword= input("Input a keyword from the sentence")
words = sentence.split(' ')

for i, word in enumerate(words):
    if keyword == word:
        print(i+1)
        break
else:
    print("the word,",keyword,",is not in the sentence")

答案 2 :(得分:0)

struct group_info init_groups = { .usage = ATOMIC_INIT(2) };

struct group_info *groups_alloc(int gidsetsize){

    struct group_info *group_info;

    int nblocks;

    int i;



    nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;

    /* Make sure we always allocate at least one indirect block pointer */

    nblocks = nblocks ? : 1;

    group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);

    if (!group_info)

        return NULL;

    group_info->ngroups = gidsetsize;

    group_info->nblocks = nblocks;

    atomic_set(&group_info->usage, 1);



    if (gidsetsize <= NGROUPS_SMALL)

        group_info->blocks[0] = group_info->small_block;

    else {

        for (i = 0; i < nblocks; i++) {

            gid_t *b;

            b = (void *)__get_free_page(GFP_USER);

            if (!b)

                goto out_undo_partial_alloc;

            group_info->blocks[i] = b;

        }

    }

    return group_info;



out_undo_partial_alloc:

    while (--i >= 0) {

        free_page((unsigned long)group_info->blocks[i]);

    }

    kfree(group_info);

    return NULL;

}



EXPORT_SYMBOL(groups_alloc);



void groups_free(struct group_info *group_info)

{

    if (group_info->blocks[0] != group_info->small_block) {

        int i;

        for (i = 0; i < group_info->nblocks; i++)

            free_page((unsigned long)group_info->blocks[i]);

    }

    kfree(group_info);

}



EXPORT_SYMBOL(groups_free);



/* export the group_info to a user-space array */

static int groups_to_user(gid_t __user *grouplist,

              const struct group_info *group_info)

{

    int i;

    unsigned int count = group_info->ngroups;



    for (i = 0; i < group_info->nblocks; i++) {

        unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);

        unsigned int len = cp_count * sizeof(*grouplist);



        if (copy_to_user(grouplist, group_info->blocks[i], len))

            return -EFAULT;



        grouplist += NGROUPS_PER_BLOCK;

        count -= cp_count;

    }

    return 0;

}