Is it possible to return variables to the main function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cameron McAulif
    New Member
    • Mar 2011
    • 3

    Is it possible to return variables to the main function?

    Hi, I'm new to python, programming in general actually. I was wondering if it was possible to return variables from a function? Hmm that probably didn't make sense, i'll give an example. Lets say i had this program:
    Code:
    def main ():
        age = input ('What is your age in years? ')
        weeksOld = age * 52
        print 'Wow, you are at least ', weeksOld, ' weeks old!!!'
    main ()
    Is it possible to separate the calculation into a function away from main and still have it return the variable for the print statement? I'll try and show you what i mean:
    Code:
    def main ():
        age = input ('What is your age in years? ')
        weeksOld (age)
        print 'Wow, you are at least ', wsOld, ' weeks old!!!'
    
    def weeksOld (years):
        wsOld = years * 52
        return wsOld
    main ()
    Sorry if that was hard to understand or if the question was rather nooby, as i said i'm new so try not to flame me too much, thanks!
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    #2
    Cameron,
    Did you try it? You could answer this by yourself :)

    Code:
    def weeksOld (years): 
        return = years * 52 
    main ()

    Comment

    • Cameron McAulif
      New Member
      • Mar 2011
      • 3

      #3
      Haha that was actually pretty simple :), thanks heaps!

      Comment

      Working...