[英]how to use while loop to determine the amount of numbers in a list that are greater the average
Hi I need to do this task in two ways: one way with for loop which I did and other with while loop but I don't secceed....The code I wrote is: 嗨,我需要以两种方式执行此任务:一种是使用for循环,另一种是使用while循环,但我没有剖析……。我编写的代码是:
A = [5,8,9,1,2,4]
AV = sum(A) / float(len(A))
count = 0
for i in A :
if i > AV :
count = count + 1
print ("The number of elements bigger than the average is: " + str(count))
count = 0
while float in A > AV:
count += 1
print ("The number of elements bigger than the average is: " + str(count))
Your code is indeed unformatted. 您的代码确实未格式化。 Generally:
通常:
for x in some_list:
... # Do stuff
is equivalent to: 等效于:
i = 0
while i < len(some_list):
... # Do stuff with some_list[i]
i += 1
The problem is that you are using while float in A > AV:
in your code. 问题是您在代码
while float in A > AV:
中使用while float in A > AV:
使用。 The while works until the condition is true. 在条件成立之前,while将起作用。 So as soon as it encounters some some number in list which is smaller than average, the loop exits.
因此,一旦列表中遇到一些小于平均数的数字,循环就会退出。 So, your code should be:
因此,您的代码应为:
A = [5,8,9,1,2,4]
AV = sum(A) / float(len(A))
count = 0
for i in A : if i > AV :
count = count + 1
print ("The number of elements bigger than the average is: " + str(count))
count = 0
i = 0
while i < len(A):
if A[i] > AV: count += 1
i += 1
print ("The number of elements bigger than the average is: " + str(count))
i hope it helped :) and i believe you know why i added another variable i
. 我希望它能对您有所帮助:)并且我相信您知道为什么我要添加另一个变量
i
。
A = [5,8,9,1,2,4]
AV = sum(A) / float(len(A))
count = 0
for i in A:
if i > AV:
count = count + 1
print ("The number of elements bigger than the average is: " + str(count))
count = 0
i = 0
while i < len(A):
if A[i] > AV:
count += 1
i += 1
print ("The number of elements bigger than the average is: " + str(count))
You can use something like the code below. 您可以使用类似下面的代码。 I have commented on each part, explaining the important parts.
我对每个部分都进行了评论,解释了重要的部分。 Note that your
while float in A > AV
is not valid in python. 请注意,
while float in A > AV
中的while float in A > AV
在python中无效。 In your case, you should access elements of a list by indexing or using a for
loop with the in
keyword. 对于您的情况,应该通过索引或使用带有
in
关键字的for
循环来访问列表的元素。
# In python, it is common to use lowercase variable names
# unless you are using global variables
a = [5, 8, 4, 1, 2, 9]
avg = sum(a)/len(a)
print(avg)
gt_avg = sum([1 for i in a if i > avg])
print(gt_avg)
# Start at index 0 and set initial number of elements > average to 0
idx = 0
count = 0
# Go through each element in the list and if
# the value is greater than the average, add 1 to the count
while idx < len(a):
if a[idx] > avg:
count += 1
idx += 1
print(count)
The code above will output the following: 上面的代码将输出以下内容:
4.833333333333333
4.833333333333333
33
33
Note: There are alternatives to the list comprehension that I've provided. 注意:我提供了列表理解的替代方法。 You could also use this piece of code.
您也可以使用这段代码。
gt_avg = 0
for val in a:
if val > avg:
gt_avg += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.