Python Forum
Need help with a simple function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a simple function
#1
Hi,

I am totally new to Python and programming. However, I tryed to create a simple function, which is not working. Please help.

def money(capital, years, added_yearly, interest):
    a = 0
    while a < years:
        capital = (capital+added_yearly)*interest
        a = a+1
        return (capital, years)

money(33000, 5, 3000, 1.1)
print("Your capital after %s years will be %s" % (years, capital))
When I am trying to run this code with PowerShell, it give me the error:

NameError: name 'years' is not defined

even though I am returning the values with my function. What am I doing wrong?
Reply
#2
Include the complete trackback so we know which line contain the error.
Reply
#3
You didn’t assign names to function result. There are no reference to object returned. You also can’t access names in function namespace from outside.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Apr-26-2019, 11:30 AM)woooee Wrote: Include the complete trackback so we know which line contain the error.

The error is in line 9.
Reply
#5
Your problem is on this row:

money(33000, 5, 3000, 1.1)
Where does value returned by function goes? There is no references to this returned object, therefore garbage collector destroys it and it's gone!

Your function returns tuple, so you can unpack it (f-strings, so Python >= 3.6 is required):

def money(capital, years, added_yearly, interest):
    a = 0
    while a < years:
        capital = (capital+added_yearly)*interest
        a = a+1
        return (capital, years)
 
capital, years = money(33000, 5, 3000, 1.1)
print(f"Your capital after {years} years will be {capital}")
Output:
Your capital after 5 years will be 39600.0
Alternatively you can unpack it when printing (I reversed order of returned values):

def money(capital, years, added_yearly, interest):
    a = 0
    while a < years:
        capital = (capital+added_yearly)*interest
        a = a+1
        return (years, capital)
 
print("Your capital after {} years will be {}".format(*money(33000, 5, 3000, 1.1)))
Output:
Your capital after 5 years will be 39600.0
You can also return namedtuple which enables doing some nice descriptive stuff.

However, while in interactive interepreter and not running file you can use underscore (interpreter remembers last value by assigning it with name _):

In [1]: def money(capital, years, added_yearly, interest): 
   ...:     a = 0 
   ...:     while a < years: 
   ...:         capital = (capital+added_yearly)*interest 
   ...:         a = a+1 
   ...:         return (years, capital) 
   ...:                                                                       

In [2]: money(33000, 5, 3000, 1.1)                                            
Out[2]: (5, 39600.0)

In [3]: "Your capital after {} years will be {}".format(*_)                   
Out[3]: 'Your capital after 5 years will be 39600.0'
As a sidenote - this is not the way to calculate capital. You will get wrong result.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  got SyntaxError while building simple function zarize 2 2,079 Feb-14-2020, 10:51 AM
Last Post: zarize
  Simple statistics with range function Pythonlearner2019 2 2,083 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Cannot get simple i/o to function. jerryi 10 6,645 Jul-27-2019, 06:22 PM
Last Post: jerryi
  Why this simple function doesnt work? blackknite 8 3,905 Jan-05-2019, 12:32 PM
Last Post: buran
  Simple Function Problem, please help ShadowWarrior17 16 6,930 Jan-03-2018, 09:29 PM
Last Post: ShadowWarrior17
  How to do multithrading of a simple function? ratanbhushan 1 2,605 Nov-17-2017, 02:32 PM
Last Post: heiner55
  Simple Function Call PappaBear 2 3,133 Apr-04-2017, 11:12 PM
Last Post: PappaBear

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020