努力理解while循环

时间:2020-08-16 12:17:44

标签: python

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_odd = 0
list_sum = 0
i = 0
len_num_list = len(num_list)

while (count_odd < 5) and (i < len_num_list): 
    if num_list[i] % 2 != 0:
        list_sum += num_list[i]
        count_odd += 1
    i += 1

print ("The numbers of odd numbers added are: {}".format(count_odd))
print ("The sum of the odd numbers added is: {}".format(list_sum))

免责声明:我是一个完整的初学者,我发现自己在while循环中苦苦挣扎。 For循环对我来说很有意义。 此代码的目的是获取列表中的前5个奇数并找到它们的和。 我正在努力了解i变量的用途以及为什么他们使用num_list[i] % 2 != 0(如果i在循环前全局设置为0)来查找奇数。我猜它模仿一个for循环,因此它可以遍历列表,但是我不确定它是如何工作的。以及下面的代码块

list_sum += num_list[i]
count_odd += 1
i += 1 

是我最努力的目标。我不明白这段代码是要实现什么以及它是如何工作的。

5 个答案:

答案 0 :(得分:1)

只要count_odd变量(到目前为止已评估的奇数个数)小于5 i(总数),您的while循环就会继续循环到目前为止已评估)小于要评估的数字列表的长度。

每次运行代码时,如果当前索引(i)上的数字为奇数(除以0后没有余数),则奇数之和(list_sum)为增加那个数字的值。同样,求值的奇数(count_odd)的数量增加1以表示另一个奇数被求值。但是,无论它是否为奇数,当前数字(i)的索引都会增加1,表示可以对下一个数字求值。

答案 1 :(得分:1)

代码很简单:

Wile的条件是,虽然他没有找到5个号码,并且在第一个列表中有号码,但是它必须起作用。

比条件:

if num_list[i] % 2 != 0:

可变性用于读取列表中的每个元素,而可变性本身会增加。

然后num%2返回 num / 2除法的余数。如果不为0,则肯定为1,因此数字为奇数。

可变性本身会增加,因为它必须读取列表中的下一个数字。如果不合适,请尝试学习有关列表,元组等的信息

希望会有用

答案 2 :(得分:0)

#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

答案 3 :(得分:0)

您可以将其转换为“ for”循环,以分隔条件检查。无需为num_list编制索引,可以对其进行迭代。

for n in num_list: 
     if n%2==0: 
         continue 
     if count_odd==5: 
         break 
     list_sum+= n 
     count_odd+= 1

答案 4 :(得分:0)

变量i是运行while循环的变量。 在代码初始i = 0中,使用(num_list [i]%2!= 0)来查找奇数。 因为任何可被2整除的数字都是偶数,因此在代码中将数字除以2时应给出它不等于零。 所以这是一个奇数。

代码说明。

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.

这是与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]

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)