努力理解这个循环

  • 本文关键字:循环 努力 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))

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

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

是我最挣扎的。我不明白这段代码的目的是什么,以及它是如何工作的。

只要count_odd变量(到目前为止求值的奇数数量(小于5,并且i(到目前求值的总数(小于要求值的数字列表的长度,while循环就会继续循环。

每次代码运行时,如果当前索引(i(处的数字是奇数(除以0后没有余数(,则奇数之和(list_sum(将增加该数字的值。此外,所评估的奇数的数目(count_odd(增加1以表明另一奇数被评估。但无论它是否是奇数,当前数字(i(的索引都会增加1,以表明可以评估下一个数字。

代码非常简单:

威尔的条件意味着,虽然他还没有找到5个数字,而且第一个列表中有数字,但它必须起作用。

比条件:

if num_list[i] % 2 != 0:

variable用于读取列表中的每个元素,而variable则自行增加。

然后num%2返回除法的余数num/2。如果它不是0,它肯定是1,所以这个数字是奇数。

变量本身会增加,因为它必须读取列表中的下一个数字。如果这对你来说不好,试着在上学习一些关于列表、元组等的知识

希望对有用

#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

您可以将其转换为分离条件检查的"for"循环。索引num_list是不必要的,它可以重复。

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

变量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)

最新更新