2016-11-14 100 views
0
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语句,即使该单词在句子中“打印”字样,“关键字”,“不在句子中”。其他语句不适用于我的if语句

+2

第一次'keyword'不等于'word'时,你的else语句被执行。 –

回答

0

else会对每个不是关键字的单词产生影响,导致许多'失败'。

使用for-else循环与break荷兰国际集团的条件,让你的程序将显示失败的输出只有如果没有字匹配关键字:

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

只是为了多一点pythonic,而不是使用一个标志,你可以使用for-else。 – Lafexlos

+0

@Lafexlos谢谢,它出现了,但我忘记了'break'如何进入 – Uriel

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") 
+0

else语句仍然会运行。然而,一个简单的解决方法是在for循环中的print语句之后添加'break'。 –

+0

我明白了。现在应该可以正常工作了。谢谢 – Inconnu

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; 

} 
+0

尝试格式化响应并更好地将代码与某些描述集成 –