Python Forum

Full Version: [split] Problem using global variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(Nov-09-2017, 08:35 AM)Mekire Wrote: [ -> ]The problem here is you never defined these three variables monthlyInvestment, yearlyInterest, years when you pass them to calculateFutureValue on line 15.  As it seems you want to take those from user input, you don't need to pass them at all.
Never defined them? I'm getting their values through user input, so how is that not defining them?

Also, here is more more trouble I'm having with my functions (the questions and errors are in the code comments):
#!/usr/bin/env python3
#MainFunctionBasics.py

def globalVarExperiment(distancePerMinute, distancePerSecond):
    distancePerMinute = hyperSonicSpeed / 60
    distancePerSecond = hyperSonicSpeed / 3600
    print("At " + hyperSonicSpeed + "mph, you travel:")
    print(distancePerMinute + "miles per minute, and "
          + distancePerSecond + "miles per second.")

global hyperSonicSpeed = 4000#Um... why is the = sign invalid syntax here?

def main():    

    globalVarExperiment()
    print()
    yearlySalary = hardCodedNamedArguments(wage, workWeek)#wage is not defined
    print()
    milesPerMinute = overrideValues(200,60)#how do I override the arguments?
    print()

    #main function definition ends here

def hardCodedNamedArguments(hourlyWage=62.5, weeklyWorkHours=40):
    weeklyPaycheck = weeklyWorkHours * weeklyWorkHours
    monthlyPaycheck = 4 * weeklyPaycheck
    yearlySalary = monthlyPaycheck * 12
    return yearlySalary

def overrideValues(500, 60):#500 is invalid syntax. Why?
    milesPerMinute = 500 / 60
    print("At " + 500 + "mph, you travel " + milesPerMinute
          + " miles per minute.")
    return milesPerMinute
    
main()
You need to assign a value to the variable before declaring it as a global

C:\>python
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> hyperSonicSpeed = 4000
>>> global hyperSonicSpeed
>>>