Python 3.x 使用while循环
我正在为一堂课做作业,作业在codingbat.com上。问题如下: 返回数组中数字的总和,空数组返回0。除此之外,数字13是非常不吉利的,所以它不算数,紧跟在13之后的数字也不算数 到目前为止,我有:Python 3.x 使用while循环,python-3.x,Python 3.x,我正在为一堂课做作业,作业在codingbat.com上。问题如下: 返回数组中数字的总和,空数组返回0。除此之外,数字13是非常不吉利的,所以它不算数,紧跟在13之后的数字也不算数 到目前为止,我有: def sum13(nums): sum = 0 i = 0 while i in range(len(nums)): if i == 13: i += 2 else:
def sum13(nums):
sum = 0
i = 0
while i in range(len(nums)):
if i == 13:
i += 2
else:
i += 1
return sum
另外,我有一个更好的代码:
def sum13(nums):
sum = 0
for i in range(len(nums)):
if nums[i] != 13:
sum += nums[i]
return sum
我知道我应该使用while循环,但我就是不明白这一点。看起来你就快到了;您只需将
nums[i]
的值实际添加到sum
的适当位置。另外,请重新考虑您的返回值行的缩进。看起来您就快到了;您只需将nums[i]
的值实际添加到sum
的适当位置。还要重新考虑返回和
行的缩进。对于
循环,通常使用而不使用范围()。这种循环(在Python中)通常用于循环元素值,而不是索引值。需要索引时,使用enumerate()
函数获取索引和元素值,如下所示(但在这种情况下不需要):
问题与索引值无关。通过这种方式,
/的解决方案与解决方案仅在访问数组元素时有所不同。您需要在
时使用索引,但对于
不需要使用索引
while
方法的问题还在于,不能简单地跳过值13之后的索引,因为下一个元素也可以包含13。您需要存储上一个元素的值,并使用它来决定是否将当前值添加到总和中。对于
和while
解决方案,其在中都是相同的。大概是这样的:
last = 0 # init; whatever value different from 13
sum = 0
the chosen kind of loop:
e ... # current element from nums
if e != 13 bool_operator_here last != 13: # think about what boolean operator is
add the element to the sum
remember e as the last element for the next loop
sum contains the result
[稍后编辑]好吧,你放弃了。以下是解决此问题的代码:
def sumNot13for(nums):
last = 0 # init; whatever value different from 13
sum = 0
for e in nums:
if e != 13 and last != 13:
sum += e # add the element to the sum
last = e # remember e as the last element for the next loop
return sum
def sumNot13while(nums):
last = 0 # init; whatever value different from 13
sum = 0
i = 0 # lists/arrays use zero-based indexing
while i < len(nums):
e = nums[i] # get the current element
if e != 13 and last != 13:
sum += e # add the element to the sum
last = e # remember e as the last element for the next loop
i += 1 # the index must be incremented for the next loop
return sum
if __name__ == '__main__':
print(sumNot13for([2, 5, 7, 13, 15, 19]))
print(sumNot13while([2, 5, 7, 13, 15, 19]))
print(sumNot13for([2, 5, 7, 13, 13, 13, 13, 13, 13, 15, 19]))
print(sumNot13while([2, 5, 7, 13, 13, 13, 13, 13, 13, 15, 19]))
def sumNot13for(nums):
last=0#init;不管值与13不同
总和=0
对于NUM中的e:
如果e!=13和最后!=13:
sum+=e#将元素添加到sum中
last=e#记住e是下一个循环的最后一个元素
回报金额
def sumNot13while(nums):
last=0#init;不管值与13不同
总和=0
i=0#列表/数组使用基于零的索引
而我
通常在不使用范围()的情况下,将用于
循环。这种循环(在Python中)通常用于循环元素值,而不是索引值。需要索引时,使用enumerate()
函数获取索引和元素值,如下所示(但在这种情况下不需要):
问题与索引值无关。通过这种方式,
/的解决方案与解决方案仅在访问数组元素时有所不同。您需要在
时使用索引,但对于
不需要使用索引
while
方法的问题还在于,不能简单地跳过值13之后的索引,因为下一个元素也可以包含13。您需要存储上一个元素的值,并使用它来决定是否将当前值添加到总和中。对于
和while
解决方案,其在中都是相同的。大概是这样的:
last = 0 # init; whatever value different from 13
sum = 0
the chosen kind of loop:
e ... # current element from nums
if e != 13 bool_operator_here last != 13: # think about what boolean operator is
add the element to the sum
remember e as the last element for the next loop
sum contains the result
[稍后编辑]好吧,你放弃了。以下是解决此问题的代码:
def sumNot13for(nums):
last = 0 # init; whatever value different from 13
sum = 0
for e in nums:
if e != 13 and last != 13:
sum += e # add the element to the sum
last = e # remember e as the last element for the next loop
return sum
def sumNot13while(nums):
last = 0 # init; whatever value different from 13
sum = 0
i = 0 # lists/arrays use zero-based indexing
while i < len(nums):
e = nums[i] # get the current element
if e != 13 and last != 13:
sum += e # add the element to the sum
last = e # remember e as the last element for the next loop
i += 1 # the index must be incremented for the next loop
return sum
if __name__ == '__main__':
print(sumNot13for([2, 5, 7, 13, 15, 19]))
print(sumNot13while([2, 5, 7, 13, 15, 19]))
print(sumNot13for([2, 5, 7, 13, 13, 13, 13, 13, 13, 15, 19]))
print(sumNot13while([2, 5, 7, 13, 13, 13, 13, 13, 13, 15, 19]))
def sumNot13for(nums):
last=0#init;不管值与13不同
总和=0
对于NUM中的e:
如果e!=13和最后!=13:
sum+=e#将元素添加到sum中
last=e#记住e是下一个循环的最后一个元素
回报金额
def sumNot13while(nums):
last=0#init;不管值与13不同
总和=0
i=0#列表/数组使用基于零的索引
而我
谢谢Ant P的帮助,我还没有计算出来,但我想说的是,在我发布后,我开始编辑我的帖子,看起来在你回答后我对它进行了更改,因此,我认为你给出的答案并不准确,因为我的解不正确。对不起,你还在检查索引(I
)是否为13,不是该索引中的元素(nums[i]
)。感谢Ant P的帮助,我还没有计算出来,但我想说的是,在我发布后,我开始编辑我的帖子,看起来像是在你回答后我更改了它,因此,我认为你给出的答案对于我的错误答案并不准确