user2955459
user2955459

Reputation: 15

1 Using the substitution method for solving recurrences 1

F(x,y)=3x+10y

I want to show all result of f(x,y) for x={0,1…,5} and y={0,1,…..,8} in python
i tried:

from math import *
def f(x,y):
    x in rang(5)
    y in rang(8)
    return (3*x)+(10*y)

NameError: name 'x' is not defined

Upvotes: 0

Views: 51

Answers (2)

ford
ford

Reputation: 11826

You seem to have the idea of how a function works mixed up. You should be looping outside the f(x,y) function and passing in x and y.

I believe that this is what you want:

from math import *

def f(x, y):
    return (3 * x) + (10 * y)

for x in range(5):
    for y in range(8):
        print "f(%d, %d) = %d" % (x, y, f(x, y))

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239463

You have to store the results in a list like this and then return at the end of the loops like this

def f(x,y):
    result = []
    for i in range(x):
        for j in range(y):
            result.append((3*i)+(10*j))
    return result

print f(5, 8)

Or you can use list comprehension to shorten the loop secion like this

def f(x,y):
    return [(3*i)+(10*j) for i in range(x) for j in range(y)]

Upvotes: 2

Related Questions