[英]Else statement not working with my 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語句,即使該單詞位於句子“ the word”,“ keyword”中,也不會出現在句子中。句子”。
else
,除關鍵字之外的每個單詞都會插入else
單詞,從而導致許多“失敗”。
使用帶有break
條件的for-else
循環,這樣, 只有在沒有單詞與關鍵字匹配時,程序才會顯示失敗的輸出:
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")
由於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")
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.