I wrote these but it doesn’t work. Can you help me
import sys
a=sys.argv[1]
b=a.split(",")
total=0
for i in b:
i=int(i)
if i<0:
b.remove(i)
t=i
n=len(i)
s=t/n
if i%2==0:
print("Even numbers:{}".format(i))
total+=i
number=len(i)
se=total/number
print("Sum of even numbers:{}".format(total/number))
print("Even number rate:{}".format(se/s))
There is a simple reason the program will fail: You are altering the object you are looping over via b.remove(). The for-in-loop still goes by index, but by removing items from the list, it will result in skipping numbers.
t=i
n=len(i)
s=t/n
Ok ehm, what is that?
You just turned i into an integer - what exactly do you think the length of an integer is? And what are you even trying to calculate here?
You are supposed to calculate the rate of even numbers. That’s a basic counting-task which doesn’t need any kind of division up until you are done with the counting.
Instead you have THREE division WITHIN the loop, which for reasons unknown includes a division to get a sum…
Like, do you even know what you are doing?
Please read the task again and then just solve it on paper, by hand. Once you succeed with that, try to turn your steps into a program.
thanks
I tried to calculate the total average there but
I just realized that isnt necessary probably because my english is not good
you said Like, do you even know what you are doing? actually no because I couldn’t understand the problem
“There is a simple reason the program will fail: You are altering the object you are looping over via b.remove() . The for-in-loop still goes by index, but by removing items from the list, it will result in skipping numbers.” how can i fix it