Python 努力理解这个while循环
这是我最挣扎的。我不明白这段代码的目的是什么,以及它是如何工作的。Python 努力理解这个while循环,python,Python,这是我最挣扎的。我不明白这段代码的目的是什么,以及它是如何工作的。#这是第一次初始化,基本上从0开始 list_sum += num_list[i] count_odd += 1 i += 1 i=0 len_num_list=len(num_list) #您可以将while重新构造为: #只要您没有5个奇数,并且没有看完整个列表,请继续 而(奇数
#这是第一次初始化,基本上从0开始
list_sum += num_list[i]
count_odd += 1
i += 1
i=0
len_num_list=len(num_list)
#您可以将while重新构造为:
#只要您没有5个奇数,并且没有看完整个列表,请继续
而(奇数<5)和(我<长数列表):
#num_list[i]表示从列表中获取索引为i的元素
#因为i是用0初始化的,所以在第一个循环中,您将获取第一个元素(在python中,索引从0开始,而不是从1开始)
#SomeNumber%2表示如果将数字除以2,小数是多少。
#只有奇数在2除法后才有小数,例如4/2=2.0,但3/2=1.5,所以其余的是5
#而5是!=0
如果数量列表[i]%2!=0:
#number+=other_number本质上是:number=number+other_number的缩写
#基本上,您有,例如,numb=100,并且想要添加10,您可以执行以下操作之一:
#麻木=麻木+10或麻木+10
list_sum+=num_list[i]
计数_奇数+=1
#因为我们现在通过了列表的第一个数字,它的索引为0
#现在,我们在索引中添加1,while循环将继续,直到满足“while”关键字后面所述的标准。
i+=1
只要count\u odd
变量(到目前为止计算的奇数)小于5并且i
(到目前为止计算的总数)小于要计算的数字列表的长度,while循环将继续循环
每次代码运行时,如果当前索引(
i
)处的数字为奇数(除以0后没有余数),则奇数(list\u sum
)之和将增加该数字的值。此外,计算的奇数(count\u odd
)的数量增加1,以表明计算了另一个奇数。但是,无论它是否是奇数,当前数字(i
)的索引都会增加1,以表示可以计算下一个数字。代码非常简单:
诡计上的条件意味着,虽然他没有找到5个数字,而且第一个列表中有数字,但它必须起作用
超过条件:
#it is the first initializing, essentially starting with 0
i = 0
len_num_list = len(num_list)
# you can reframe the while to:
# as long as you do not have 5 odd numbers AND you are not through the whole list, continue
while (count_odd < 5) and (i < len_num_list):
# num_list[i] means that you take the element with index i from the list
# since i is initialized with 0, at the first loop you take the first element (in python index starts at 0 not 1)
# SomeNumber % 2 means that if you divide the number by 2, what are the decimals.
# Only odd numbers will have decimals after division with 2, e.g. 4/2 = 2.0 but 3/2 = 1.5 so the rest would be 5
# and 5 is != 0
if num_list[i] % 2 != 0:
# number += other_number is essentially short for: number = number + other_number
# essentially you have e.g. numb = 100 and want to add 10, you can either do:
# numb = numb + 10 OR numb += 10
list_sum += num_list[i]
count_odd += 1
# since we are now through the first number of the list, which had index 0
# we now add 1 to the index and the while loop continues until the criterias stated after the "while" keyword are met.
i += 1
变量用于读取列表中的每个元素,而变量本身会增加
然后num%2返回
除法num/2的剩余部分。如果不是0,则肯定是1,因此该数字为奇数
变量本身会增加,因为它必须读取列表中的下一个数字。如果不适合您,请尝试学习有关列表、元组等的知识
希望将非常有用您可以将其转换为“for”循环,用于分隔条件检查。为num_列表编制索引是不必要的,可以对其进行迭代
if num_list[i] % 2 != 0:
变量i是运行while循环的变量。 在代码中,初始值i=0,使用(num_list[i]%2!=0)查找奇数。 因为任何可被2除的数都是偶数。因此,在代码中,当数被2除时,它不应等于零。 所以这是一个奇数 代码的解释
for n in num_list:
if n%2==0:
continue
if count_odd==5:
break
list_sum+= n
count_odd+= 1
下面是与for循环相同的代码
num_list=[422、136、524、85、96、719、85、92、10、17、312、542、87、23、86、191、116、35、173、45、149、59、84、69、113、166]
count奇数=0
总和=0
l=len(数量列表)
对于范围(l)中的i:
如果countodd@S0V22非常欢迎。请投票支持我的回答。
for n in num_list:
if n%2==0:
continue
if count_odd==5:
break
list_sum+= n
count_odd+= 1
num_list[0]=422
422/2 is equal to zero.so it is not a odd number.
Then the i value is incriminating by 1
So i=i+1
i=0+1
i=1
now the value of i=1
num_list[1]=136
136/2=0 it is a even number.
Now i will again incriminating by 1
i=1+1
i=2
num_list[2]=524
524 is again even.
Then i again incriminating by 1
i=2+1
i=3
num_list[3]=85
85/2 is not zero
then list_sum=0+85
now list_sum=85
And i keeps on incriminating by 1 till 5 odd numbers is achieved.
num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
countodd=0
sum=0
l=len(num_list)
for i in range(l):
if countodd<5 and num_list[i] % 2 != 0:
sum+=num_list[i]
countodd+=1
print("The numbers of odd numbers are",countodd)
print("The sum of the odd numbers is",sum)