New to python programming, when executing i get: print_target_output(principal,rate,years) NameError: name 'years' is not defined
What i want to do is to calculate years and print it
Any idea how do i fix this?
def calculate_years_to_target(principal, rate, target):
years = math.log(target/principal) / 4*(math.log(1 + rate/4))
return years
def print_target_output(principal, rate, years):
print(principal,rate,years)
#MAIN
def main():
print_intro()
principal,rate,target=get_target_input()
calculate_years_to_target(principal,rate,target)
print_target_output(principal,rate,years)
You need to assign your returned result to a variable when you call the function. If you do not do so, it will not be stored outside the scope of your function.
def main():
print_intro()
principal,rate,target=get_target_input()
## Assign the returned result to a variable ##
years = calculate_years_to_target(principal,rate,target)
print_target_output(principal,rate,years)
To further your learning it may be of benefit to spend some time better understanding scopes and when you can/cannot access variables you assign in your code.