0

I want to use a for loop to define a range and I would like to use a while loop to check a condition for every value inside this for loop and give me the results for different values of c, but unfortunately, my algorithm doesn't work and I don't know what my mistake is.

j=0
jl=[]
c=np.linspace(0,20,num=20)
for a in range(0,len(c)):
    while j<5:
        j=c[a]+2
    jl.append(j)

The result I am looking for is it puts different values of c inside the while loop and calculate j and check if it is bigger than 5 or not. if yes, it appends it to jl. Totally, I want to define a range with for loop with index and also check each value of this range inside while loop and get the results for j. so expected results are j with values smaller than 5 (c[a]+2<5) and store the values of j in jl

11
  • 3
    You start with j=0 and your while condition is j>5 so it never runs. Commented Feb 24, 2019 at 19:37
  • @wwii sorry i wrote down wrongly here, i corrected it Commented Feb 24, 2019 at 19:39
  • Your example has jl.append(j) outside/after the for loop - is that intended? - it will only run once after the for loop is done. Commented Feb 24, 2019 at 19:42
  • @wwii actually I am new in programming, especially python, that's why I am not aware of my mistakes, this task is very important for me because I am going to write a program in the university. Actually, I don't know where should i put this append right now. I didn't get any results. Commented Feb 24, 2019 at 19:44
  • 1
    You don't actually need the range here, because you never try to assign to c[a] or use a for anything other than indexing c. Just iterate over c directly: for item in c: while j < 5: j=item + 2; .... Commented Feb 24, 2019 at 19:52

3 Answers 3

1

There is one problem in your code:

j=0

"While loop" never runs because his condition is j>5. Correct that and tell us if it works.

Sign up to request clarification or add additional context in comments.

Comments

1

Please double check if the following suggested solution covers all of your requirements. In any case I think List Comprehensions (e.g. compare section 5.1.3.) can help you.

c=np.linspace(0,20,num=20)
ls = [x+2 for x in c if((x+2)<5)]
print(ls)

Will result in the following output:

[2.0, 3.052631578947368, 4.105263157894736]

If you want to do more complex data manipulation, you can also do this with the help of functions, e.g. like:

def someManipulation(x):
    return x+2

ls = [someManipulation(x) for x in c if(someManipulation(x)<5)]

Comments

1

Your algorithm doesn't work because

while j<5:
    j=c[a]+2

is an infinite loop for j = 0, a = 0.

What you probably wanted to write was:

for x in c:
    j = x + 2
    if j < 5:
        jl.append(j)

Still, the list comprehension version in the other answer does exactly the same, but better.

Comments

Your Answer

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.