1

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])
asked Jul 22, 2019 at 10:45
2
  • 2
    It'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. Commented Jul 22, 2019 at 10:47
  • 1
    Use and instead of or Commented Jul 22, 2019 at 10:48

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Clean solution. It will make the job.
0

You should use an and:

if consensusSize[i]>50 and consensusSize[i]<200:
answered Jul 22, 2019 at 10:47

2 Comments

but then it prints nothing. There is definitely items in the list that fulfill the rule. e.g. 178,7,63
What is the value of consensusSize? I just tried it on the console and it works.
0

Use "and" in line 2 instead of "or".

if consensusSize[i]>50 and consensusSize[i]<200:
answered Jul 22, 2019 at 10:49

Comments

0
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

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.