Python Forum
[split] Problem using global variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Problem using global variables
#1
(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()
Reply
#2
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
>>>
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,121 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,028 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,149 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 947 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,732 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,655 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,261 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  [split] Python Class Problem astral_travel 12 4,984 Apr-29-2020, 07:13 PM
Last Post: michael1789
  Question regarding local and global variables donmerch 12 5,095 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,441 Apr-08-2020, 06:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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