简体   繁体   中英

Query about print statement inside while loop

In the code below, I thought what I wrote would lead it to print the values of y x_1 and x_0 after every iteration but instead I get nothing.

Why doesn't the code print anything when it runs?

G = 6.6741*10**-11
r_e = 6371000
r_m = 1737100
M_e = 5.9722*10**24
M_m = 7.3420*10**22
R = 3.8440*10**8
w =  2.6617*10**-6

def f(x):
     return (G*M_e)/x**2 - (G*M_m)/(R-x)**2 - w**2 * x

x_0=2*10**8
x_1=2.2*10**8

i=1

while i<=10 and abs(x_1-x_0)<abs(x_1)*1e-15:
    y=x_1-f(x_1)*(x_1-x_0)/(f(x_1)-f(x_0))
    x_0=x_1
    x_1=y
    i+1
    print (y,x_0,x_1)

The second condition abs(x_1-x_0)<abs(x_1)*1e-15 is False before the first iteration of the while loop. That is the reason why no print statement is executed.

Probably you wanted to have as second condition abs(x_1-x_0)>1e-15 .

In addition, your code is not a minimal working example, because the constants(?) G, M_e, M_m, R and w are not defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM