I am entering a while loop with a specific initial condition (m=0). Inside the while loop I have a for loop which needs to break if a spec. condition is reached, here if my growth rate is >= 20%.
If this condition is reached I want to leave the for loop and use i to set the new m. Then I want to do a new for loop and again leave the loop if the condition is reached and use the new i for m.
I tried with m+=i but this sums up the i's, which I don't want. I want the every new i is used for m. Please find below the code
nvdia=pd.read_csv(r"/NVDA.csv",sep=",").round(1)
nvdia["Date"]=pd.to_datetime(nvdia["Date"])
nvdia=nvdia.set_index(nvdia["Date"])
nvdia=nvdia.drop("Date",1)
nvdia=nvdia.loc["2017-07-01":"2018-07-01"]
#nvdia["Close"].plot()
m=0
i=0
while m<len(nvdia.index):
m+=i ###This is what I use but it is wrong. If I use i=m the loop is goes infinity, which really is strange
for i in range(m,len(nvdia.index)):
percentage_growth=100*(nvdia["Close"].values[i]/nvdia["Close"].values[m]-1)
if percentage_growth>=20:
break
#zmf: Thank you it helped. However I needed to add an additional break statement to leave the while loop, since my condition m
In case someone might have one day same/similar issue, here is the solution
m=0
while m<=len(nvidia.index):
for i in range(m,len(nvidia.index)):
percentage_growth=100*(nvidia["Close"].values[i]/nvidia["Close"].values[m]-1)
if percentage_growth>=20:
m=i
print(m,percentage_growth)
break
else:
percentage_growth=100*(nvidia["Close"].values[i]/nvidia["Close"].values[m]-1)
if i+1==len(nvidia.index):
break
Related
Create random numbers of 150 and take a uniform sample of 78
import numpy as np
population_data=np.random.randint(1,600,150) # 150 random numbers(integers) genereated
sample_data=[]
sample_lenth=78
p=30/len(population_data)
for i in range(1,len(population_data)):
if np.random.random() <=p:
sample_data.append(population_data[i])
print(i,len(sample_data))
if sample_lenth==len(sample_data):
break;
else:
i=10 # (basically wants to change the i value lower that for loop keep running )
print(i)
print(len(sample_data))
print(sample_data)
for loop is running still 150 which is valid and I cant add more range in for loop as if i>150 then sample_data.append(population_data[i] ) will be out of range.
What I want to achieve is:
if sample_lenth==len(sample_data): then break else change the i value to any in between 1-150 that loop continues
Any help !!
I'm afraid the for...else construct has nothing to do with what you are looking for.
The else clause is executed at most once, after the for loop has finished.
See e.g. https://book.pythontips.com/en/latest/for_-_else.html
You cannot change the i variable inside a for loop. Probably you want to use a while loop. See e.g. How to change index of a for loop?
I'm trying to implement the SOR algorithm in Python 3.X in order to solve an electrostatics problem. But, when inside the while loop, the break condition (relative error between previous and current iteration) appears to be fulfilled in the first iteration, which is certainly not correct. When troubleshooting the problem I found that the previous and current iterations have matching values:
def solve_laplace_SOR(w,tolerance,cant_puntos,step,volt):
phi_vec=np.zeros(cant_puntos)# zero seed vector, stores previous iteration
phi_vec_k1=np.zeros(cant_puntos) #Stores current iteration
b_vec=load_ind_term(volt,cant_puntos) #independent term
E_vec=np.zeros(cant_puntos) #Electric field
R_error=100 #Initializes relative error condition so as to enter the loop
i=0 #counts iterations made
#Initializes boundary conditions
phi_vec_k1[0]=volt/2
phi_vec_k1[cant_puntos-1]=-volt/2
while R_error > tolerance:
phi_vec=phi_vec_k1[:] #Stores previous value as a copy, I checked this using id() function
phi_vec_k1[1]=((b_vec[0]-phi_vec[2])/(-2)-phi_vec[1])*w+phi_vec[1]
for x in range(2,cant_puntos-3):
phi_vec_k1[x]=((b_vec[x-1]-phi_vec[x+1]-phi_vec_k1[x-1])/(-2)-phi_vec[x])*w+phi_vec[x]
E_vec[x]=(-(phi_vec_k1[x+1]-phi_vec_k1[x-1])/(2*step))
pass
phi_vec_k1[cant_puntos-2]=((b_vec[cant_puntos-2]-phi_vec_k1[cant_puntos-3])/(-2)- phi_vec[cant_puntos-2])*w+phi_vec[cant_puntos-2]
E_vec[cant_puntos-2]=(-(phi_vec_k1[cant_puntos-1]-phi_vec_k1[cant_puntos-3])/(2*step))
E_vec[1]=(-(phi_vec_k1[2]-phi_vec_k1[0])/(2*step))
R_error=np.linalg.norm(np.array(phi_vec_k1)-np.array(phi_vec),1)/np.linalg.norm(phi_vec_k1,1)
i+=1
print(phi_vec_k1) #This outputs the same values
print(phi_vec)
pass
return E_vec
When setting an always true break condition, the function converges to expected values. Since i'm new to python and programming in general, i can't seem to find the reason for the unwanted update. I really hope you can, thanks very much!
Please look at following while loop code written in Python:
x=25
epsilon=0.01
high=max(1.0,x)
low=0.0
*ans=(low+high)/2.0*
while abs(ans**2-x)>=epsilon:
if ans2>x:
high=ans
else:
low=ans
*ans = (high + low)/2.0*
print("ans:",ans,)
This is a guess loop (exhaustion), it should find the approx for square root of a positive number within the margin error on 0,01.
But I cant understand why we must define ans (ans=(low+high)/2.0) the second time, first before the loop and then again in the loop. Could someone tell me what purpose the second definition have since im seeing the first one being enough?
Thanks
Arif
It's because you need to perform that calculation on each iteration of the loop including the very first iteration. Since your while test is the very first part of the loop, you need to do it once before the loop starts.
Here's a way to do it with just one statement:
while True:
*ans = (high + low)/2.0*
if abs(ans**2-x)>=epsilon:
break
if ans2>x:
high=ans
else:
low=ans
something is not going right in my code below. I have a big for loop which handles some math. But in the for loop the following guys contribute to the end result.
say:
for i in range(N):
# The math goes here.
sumrfactor=0.0
for k in R_factor:
sumrfactor += k
# print(sumrfactor)
Rfactor_sum=0
for n in signal:
Rfactor_sum +=n
#print(Rfactor_sum)
r_factor = sumrfactor/Rfactor_sum
rfactor = [r_factor.copy()]
export = open('note.txt','w')
for n in rfactor:
export.write(str(n))
For each iteration I want to copy r_factor I need it for some further analysis, so I am expecting to get N number of r_factor at the end of the big for loop. But for some weird reason I keep getting the final value at the end of the mighty for loop. i.e. I get one value instead of an array. Please guys I need your help with this issue, I have no idea where the problem is at. When I open the note file there is always a single figure which obviously does not look right. Thanks guys in advance!
Your problem is that you don't append the value to the list. Change the line rfactor = [r_factor.copy()] to this:
rfactor.append(r_factor)
Below is the problem I'm trying to solve:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
It is the 5th problem from Project Euler (http://projecteuler.net/problem=5). I wrote a code in Python to find out the number, but I'm unable to obtain a solution.
My code is:
def smallestNumber():
smallest=0 ## Initializing the smallest number
end=False ## Initializing the break condition for lower level For loop
found=False ## Initializing the break condition for upper level For loop
for i in range(21,10000000): ## Upper level for loop
if i==10000000 and found==False: break ## This will break upper loop when range is satisfied
for k in range(1,20): ## Lower level for loop
if end==True: break ## This will break lower loop when range is satisfied
if i%k==0: ## If condition to check whether current i value is divisible evenly by current k value
if k==20: ## If k==20, this will make smallest equal to current i value and make both break conditions True
smallest=i
end=True
found=True
k=k+1
else: ## if not divisible, this will increment upper level loop
break
i=i+1
if found==False: print 'No value exists in this range'
else: return smallest
(I'm new to stackoverflow and was unable to paste the actual code without messing up the formatting. I apologize for any inconvenience due to that).
I keep getting the output 'No value exists in this range' regardless of how big I make my range. I'm guessing that although my logic is alright, I have messed up the code somewhere since I'm a Python beginner.
It'd be great if someone can help me.
Thanks
Some things that are wrong:
The answer is greater than your upper limit of 10000000
Your should use xrange in Python 2, otherwise you'll have memory errors if you increase the upper limit
If you want all of the numbers from 1 to 20, you should use range(1, 21)
You should not manually increase loop counters, range or xrange do it for you
First of all, what you need is LCM of the numbers from 1 to 20. You can find better ways to find LCM than the one you've implemented in this code.
Next, there are a few mistakes here. I've edited your code to illustrate. Please see my code bellow, this shows the changes I had to make in order to make your code produce the correct result for finding the smallest number divisible by all numbers from 1 to 10:
def smallestNumber():
smallest=0
end=False
found=False
for i in range(21,1000000):
if found==True: break # This will break upper loop when number is found
for k in range(1,11): # the range is up to 11 here because we need to check numbers 1-10
if end==True: break
if i%k==0:
if k==10: ##
smallest=i
end=True
found=True
#k=k+1 # not necessary
else:
break
#i=i+1 # not necessary
if found==False: print 'No value exists in this range'
else: return smallest
As you can see, I had to comment out lines like i=i+1 because that is already being taken care of by using for i in range(21,10000000).
You would also notice that I had to change if i==10000000 and found==False: break to if found==True: break because checking for i=10000000 is not necessary and you should actually stop searching when your found is true. And that was the real bug in your code. Hope this helps.