Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why do I have to define a function using python code def for the formatted output to work?

this code works:

def f(x,y):return (x-y)/(y+1)

#f(x,y)=(x-y)/(y+1)

h=0.1; n=10
x0=0;  y0=1
xn=x0; yn=y0
print("n      xn       yn")
print("-----------------------")
print("{:3.0f}{:7.1f}{:12.6f}".format(0,x0,y0))
for i in range(1,n+1):
    k1 = f(xn,yn)
    yn = yn+k1*h
    xn = xn+h
    print("{:3.0f}{:7.1f}{:12.6f}".format(i,xn,yn))

If you comment the def line and uncomment the f(x,y) line it won't output the formatted output, but would output print(i,xn,yn). It looks like the declaring of the function needs to be in python even though the loop works for both.

Why is this? C