I am having problems with a for loop. I have a list consensusSize which contains a range of random numbers. I only want to print if the number is greater than 50 and less than 200. Instead it is outputting all of them- want am I doing wrong?
for i in range(0, len(consensusSize)):
... if consensusSize[i]>50 or consensusSize[i]<200:
... print(consensusSize[i])
-
2It's already in your own writing: I only want to print if the number is greater than 50 and less than 200. Compare this with your conditional.Nelewout– Nelewout2019年07月22日 10:47:33 +00:00Commented Jul 22, 2019 at 10:47
-
1Use and instead of ormdasari– mdasari2019年07月22日 10:48:53 +00:00Commented Jul 22, 2019 at 10:48
4 Answers 4
You should think of the python for-loop as a for-each loop:
for i in consensusSize:
if 50 < i < 200:
print(i)
This would simplify your solution.
answered Jul 22, 2019 at 11:21
Teodor Ivanov
1941 gold badge1 silver badge8 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
nicor88
Clean solution. It will make the job.
You should use an and:
if consensusSize[i]>50 and consensusSize[i]<200:
answered Jul 22, 2019 at 10:47
0bero
1,0641 gold badge12 silver badges20 bronze badges
2 Comments
vanilla_2020
but then it prints nothing. There is definitely items in the list that fulfill the rule. e.g. 178,7,63
0bero
What is the value of consensusSize? I just tried it on the console and it works.
Use "and" in line 2 instead of "or".
if consensusSize[i]>50 and consensusSize[i]<200:
Comments
x_list=[10,20,30]
y_list=[12,10,23]
for x,y in zip(x_list,y_list):
print(x,y)
Hryhorii Pavlenko
3,9104 gold badges21 silver badges38 bronze badges
answered Jul 22, 2019 at 11:02
sathyanarayaana yadav
11 bronze badge
1 Comment
Vaibhav Vishal
Providing code is good but you should provide some explanation too why your code works and what mistakes(if any) the person asking questions was making. From review
lang-py