Python Forum
Trouble calling functions in main function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble calling functions in main function
#1
I'm trying to learn how to call functions in a main function, going at it with the same approach as C++ (where the main function calls all other functions defined outside of it). Here's my code:
#!/usr/bin/env python3
#MainFunctionBasics.py

def definedOutsideOfMain():
    favoriteColor = input("What is your favorite color? ")
    print("Your favorite color is " + favoriteColor + ".")
    print("This function was defined outside of the main function.")
        
def main():    

    definedOutsideOfMain()
    print()    
    definedOutsideOfMainAgain()
    print()
    futureValue = calculateFutureValue(monthlyInv, yearlyInt, numYears)
    print("The future value is: " + futureValue)
    print()

    #main function definition ends here

def definedOutsideOfMainAgain():
    favoriteBeer = input("What is your favorite beer? ")
    print("Lets go drink some " + favoriteBeer + ".")
    print("This function was also defined outside of the main function.")

def calculateFutureValue(monthlyInvestment, yearlyInterest, years):
    monthlyInvestment = float(input("Enter the monthly investment: "))
    yearlyInterest = float(input("Enter the yearly interest rate: "))
    years = int(input("Enter the number of years: "))
    #convert yearly values to monthly values:
    monthlyInterest = yearlyInterest / 12 #/ monthlyInvestment - wtf is this?
    months = years * 12
    #calculate future value:
    futureValue = 0.0
    for i in range(0, months):#display starting at 0
        futureValue += monthlyInvestment
        monthlyInterest = futureValue * monthlyInterest
        futureValue += monthlyInterest
    return futureValue    
    
main()
The errors are:
Traceback (most recent call last):
  File "I:/Python/Python36-32/SamsPrograms/MainFunctionBasics.py", line 41, in <module>
    main()
  File "I:/Python/Python36-32/SamsPrograms/MainFunctionBasics.py", line 15, in main
    futureValue = calculateFutureValue(monthlyInv, yearlyInt, numYears)
NameError: name 'monthlyInv' is not defined
First of all, why can't I call the main function where it is right now (if that's even the problem)?
Second, I thought name miss-matches for arguments in a calling statement and in a function definition didn't matter, as long as those arguments were in the same sequence. So What is wrong with my function call in line 15?
Reply


Messages In This Thread
Trouble calling functions in main function - by RedSkeleton007 - Nov-09-2017, 06:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 870 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  calling external function with arguments Wimpy_Wellington 7 1,517 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 848 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Run a Function?: Trouble with Online Python Course webmanoffesto 3 1,034 Aug-18-2022, 10:14 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 2,107 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Function global not readable by 'main' fmr300 1 1,379 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,867 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,832 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  [Solved] TypeError when calling function Laplace12 2 2,931 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,505 Jan-17-2021, 03:02 AM
Last Post: Jeremy7

Forum Jump:

User Panel Messages

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