Jul-10-2018, 02:25 AM
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.