1

I'm trying to get some help in returning values from "year_calc" function below (Python 3.5). In essence, the code works for returning "b" as I need a new starting value for "b" to be passed to "year_calc" for each iteration - I can get that to work just fine. However, I want the "total_cost" value from each year_calc iteration to be returned and added up until finished. Note the "grand_total" under the while loop. I realize this doesn't work as stated - just adding it so it may add clarity to what I'm attempting to accomplish. I am just not sure how to pull a specific value which is being returned. Any insight?

def main():
    archive_total = float(input('Enter archive total (GB): '))
    transfer_rate = float(input('Enter transfer rate (Gbps): '))
    days_to_transfer = ((((archive_total*8/transfer_rate)/60)/60)/24)
    xfer_per_day = archive_total/days_to_transfer
    day_cost = xfer_per_day * 0.007 / 30
    days = 365
    total_days = 0
    sum = 0
    b = xfer_per_day * 0.007 / 30
    total_years = 1
    grand_total = 0.0
    total_cost = 0.0
    while total_years < years_to_transfer + 1:
        b = year_calc(day_cost, days, total_days, sum,b,total_years,total_cost)
        total_years += 1
        grand_total += total_cost

def year_calc(day_cost,days,total_days,sum,b,total_years,total_cost):
    while total_days < days -1:
        b += day_cost
        sum += b
        total_days += 1
        total_cost = sum + day_cost
    print('Year',total_years,'cost: $', format(sum + day_cost, ',.2f'))
    return (b, total_cost)

main()
dpx
  • 117
  • 2
  • 10
  • I _think_ you want `b, total_cost = year_calc(day_cost, days, total_days, sum,b,total_years,total_cost)`. But I'm not sure why you pass `total_cost` as an arg to `year_calc`. BTW, you don't need the parentheses in `return (b, total_cost)`. Also, you shouldn't use `sum` as a variable name, as that shadows the built-in `sum` function. It won't hurt anything here, unless you try to use that `sum` function inside one of your functions that redefine it. – PM 2Ring May 28 '16 at 21:29

3 Answers3

1

If I understand correctly your description correctly, this implements what you want:

def main():
    # ...
    total_cost = 0.0
    while total_years < years_to_transfer + 1:
        b, total_cost = year_calc(day_cost, days, total_days, sum,b,total_years,total_cost)
        # ...

def year_calc(day_cost,days,total_days,sum,b,total_years,total_cost):
    # ...
    return (b, total_cost)

main()
nwk
  • 4,004
  • 1
  • 21
  • 22
1

year_calc, as with any function that returns multiple items, will return its values in a tuple. Therefore, you can just change this line:

b = year_calc(day_cost, days, total_days, sum,b,total_years,total_cost)

to this one:

b, total_cost = year_calc(day_cost, days, total_days, sum,b,total_years)

This works because of how Python handles multiple assignment:

>> a, b = 1,2
>> print a
1
>> print b
2

As an aside, you should try to avoid using builtin names like sum for your variables. And I'm not sure what years_to_transfer is - do you define that elsewhere in your code?

Jeremy Weirich
  • 387
  • 5
  • 20
1

Hmm, seems a bit like trying to code VBA in Python... :-)

Ok, first: I don't think you want to pass total_cost to the function year_calc, as you do not depend on any value you get. So remove it from the definition line:

def year_calc(day_cost,days,total_days,sum,b,total_years):
    ...

Next: you calculate a new value for total_cost and return a tupple from the function. That's pretty correct.

Now, when callin year_calc you should remove the total_cost variable from calling the function. But you should remember that you return a tupple, so assign the value to a tupple:

(b, total_cost) = year_calc(day_cost, days, total_days, sum,b,total_years)

Bottom line: there are no ref variables (or output variables, ...) to be sent in a function in python. Put in the parameters, nothing more. If you want to return 2 different calculations, return a tupple, but assign the value also to a tupple. Much cleaner.

jjm
  • 503
  • 4
  • 6