0

I have a function that contains several variables, but I only want to run it for one variable at the time. For example only for "exrate". The idea is to pass "exrate" into the function and then for-loop "exrate" only and calculate the result. Unfortunately the the value "turnover" does not change.

exrate = 100
sales = 0

def sim(var, min, max):
    for var in range(min, max):
        turnover = 1000 * (exrate/100) + sales
        print(turnover)

sim(exrate, 100, 105)
sim(sales, 1000, 1100)
7
  • Not sure what your expected output is or what you want? Commented Apr 22, 2018 at 14:49
  • 1
    Where is turnover defined? the variable var is exrate inside the function. You may first need to clean up variable names. Commented Apr 22, 2018 at 14:49
  • Shouldn't you use self.exrate and self.sales in the function you define? Commented Apr 22, 2018 at 14:50
  • Show your expected output. Commented Apr 22, 2018 at 14:53
  • Your current function doesn't use the var argument it's passed because it creates a local variable of the same name. What would you want the function to do with the variable (or variable name) it's passed? Commented Apr 22, 2018 at 14:57

2 Answers 2

1

Let's try to refactor your original code a bit:

exrate = 100
sales = 0

def sim(var, min, max):
    for var in range(min, max):
        turnover = 1000 * (exrate/100) + sales
        print(turnover)

First, we can factor out the function used in sim, which would be:

def f(exrate, sales):
    return 1000 * (exrate/100) + sales

and that function can be simplified further 1000/100=10:

def f(exrate, sales):
    return 10 * exrate + sales

You're using globals exrate=100, sales=0 but that's not a good idea, so lets' get rid of those globals by just using default parameters:

def f(exrate=100, sales=0):
    return 10 * exrate + sales

Now, at this point we've got a mathy function we can use as input to another functions, it's a function with one single responsability.

So let's say we want to see how this function evolves with respect to one of its independent variables (exrate or sales):

for i in range(100, 1000, 100):
    print(f(exrate=i))

for i in range(0, 1000, 200):
    print(f(sales=i))

Or both:

for i in range(0, 1000, 200):
    print(f(exrate=200, sales=i))

The main idea would be, when simulating something is a good idea to split the code into code that does the simulation (plotting the function in a graph, print values onto a console, ...) and the code which is the simulation itself (in this case, a simple linear function of the form f(x)=ax+b)

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

1 Comment

Thank you and also for the good explanation! My issue is solved.
0

You are using a variable external to the function in the definition. Consider replacing exrate with var within the function definition.

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.