Nov-09-2017, 06:46 AM
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:
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?
#!/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 definedFirst 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?