Python Forum
How do I print a returned variable calculated in another function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I print a returned variable calculated in another function?
#1
Hi all. My comprehension of how to use scope is not clicking. From the following code:

#!/usr/bin/env python3
#PracticePythonExercise01.py

#Create a program that asks the user to enter their name and their age.
#Print out a message addressed to them that tells them the year that they
#will turn 100 years old.

def yearThatYouTurn100():
    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
    year = int(input("Enter the current year: "))
    yearsBefore100 = 100 - age
    yearAt100 = year + yearsBefore100
    return yearAt100

def main():
    yearThatYouTurn100()
    print(yearAt100)

main()
I get the error:
Error:
>>> ================================ RESTART ================================ >>> Enter your name: sam Enter your age: 30 Enter the current year: 1988 Traceback (most recent call last): File "F:/Python/Python36-32/SamsPrograms/PracticePythonExercise01.py", line 20, in <module> main() File "F:/Python/Python36-32/SamsPrograms/PracticePythonExercise01.py", line 18, in main print(yearAt100) NameError: name 'yearAt100' is not defined >>>
No I don't want to print yearAt100 in the function that calculated it, because I'm still struggling to learn why I can't get the main function to recognize it.
Reply
#2
yearAt100 is local to the function yearThatYouTurn100. Once the function is over (after "return"), the variable ceases to exist for the rest of the program.

So what you should do is store the returned result in a variable, which you can then print:

def main():
    result = yearThatYouTurn100()
    print(result)
result will hold the value returned by the function.
This principle works generally for functions, and for any data types you might want to return.
Reply
#3
you can also declare the variable "yearAt100" globally and access it anywhere in the program,

#!/usr/bin/env python3
#PracticePythonExercise01.py
 
#Create a program that asks the user to enter their name and their age.
#Print out a message addressed to them that tells them the year that they
#will turn 100 years old.
yearAt100 = 0
def yearThatYouTurn100():
    global yearAt100
    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
    year = int(input("Enter the current year: "))
    yearsBefore100 = 100 - age
    yearAt100 = year + yearsBefore100
    return yearAt100
 
def main():
    yearThatYouTurn100()
    print(yearAt100)
 
main()
Reply
#4
(Jul-10-2018, 11:26 AM)Prabakaran141 Wrote: you can also declare the variable "yearAt100" globally and access it anywhere in the program,
Using globals is considered bad practise and generally discouraged. Please, don't suggest that and don't use it in your code. And just for completeness - if you do use globals, your function does not need the return statement. But again, don't do that.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 290 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
  How to access values returned from inquirer cspower 6 781 Dec-26-2023, 09:34 PM
Last Post: cspower
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,283 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 936 Aug-07-2023, 05:58 PM
Last Post: Karp
  Print variable without '' and spaces arnonim 1 722 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  How to print variables in function? samuelbachorik 3 902 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Problem with print variable in print.cell (fpdf) muconi 0 655 Dec-25-2022, 02:24 PM
Last Post: muconi
  How to calculated how many fail in each site(s) in csv files SamLiu 4 1,286 Sep-26-2022, 06:28 AM
Last Post: SamLiu

Forum Jump:

User Panel Messages

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