'NameError: name 'name' is not defined
In this code, how can I use the (name) variable inside of any other function Knowing that the variable is a local variable? I would really love some pointers if you have any, thank you.
def get_Name():
name = input("What's Your Name? ")
return f"{name} Is A Male. "
def get_Age():
age = input("What's Your Age? ")
return f"{name} Is {age} Years Old"
get_Name()
get_Age()
Solution 1:[1]
You can use a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def get_name():
return f"{self.name} Is A Male. "
def get_age():
return f"{self.name} Is {self.age} Years Old"
person = Person(
name=input("What's Your Name? "),
age=input("What's Your Age? "),
)
person.get_name()
person.get_age()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | François B. |
