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


Messages In This Thread
How do I print a returned variable calculated in another function? - by RedSkeleton007 - Jul-10-2018, 02:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 330 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Variable for the value element in the index function?? Learner1 8 711 Jan-20-2024, 09:20 PM
Last Post: Learner1
  How to access values returned from inquirer cspower 6 891 Dec-26-2023, 09:34 PM
Last Post: cspower
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 632 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,401 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,004 Aug-07-2023, 05:58 PM
Last Post: Karp
  Print variable without '' and spaces arnonim 1 745 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  How to print variables in function? samuelbachorik 3 935 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Problem with print variable in print.cell (fpdf) muconi 0 689 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,323 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