Reputation: 77
My task is to:
This is my code so far:
def check_age(age):
"""This function checks wether the person is allowed to drink based on age."""
if age >= 18:
return "allowed"
else:
return "not allowed"
def enter_scrollbar():
"""This function checks for name and age."""
name = input("What is your name? ")
age = int(input("How old are you? "))
check_age(age)
message = f"Welcome {name}. You are {legal} to drink beer."
enter_scrollbar()
I think I am having some trouble calling the functions or defining them. I get this error message:
NameError: name 'age' is not defined
Upvotes: 0
Views: 743
Reputation: 5075
You are defining age
variable inside the function. That is why it is not accessible outside.
Here is a solution;
Make the age variable global. In python you can use global
keyword to make a variable global so that it is accessible even outside the function.
Here is the code;
def enter_scrollbar():
global age
"""This function checks for name and age."""
name = input("What is your name? ")
age = int(input("How old are you? "))
def check_age(age):
"""This function checks wether the person is allowed to drink based on age."""
if age >= 18:
return "you are allowed"
else:
return "you are not allowed"
enter_scrollbar()
print(check_age(age))
Upvotes: 1
Reputation: 2237
The problem is that your not defining age out of the funtion. You need to return name and age and set them to a variable, if that makes sense. Try this:
def check_age(age):
"""This function checks wether the person is allowed to drink based on age."""
if age >= 18:
return "allowed"
else:
return "not allowed"
def enter_scrollbar():
"""This function checks for name and age."""
name = input("What is your name? ")
age = int(input("How old are you? "))
return name, age
name, age = enter_scrollbar()
legal = check_age(age)
message = f"Welcome {name}. You are {legal} to drink beer."
print(message)
Upvotes: 1
Reputation: 2100
Guesstimating that it is an indentation issue
def check_age(age):
"""This function checks wether the person is allowed to drink based on age."""
if age >= 18:
return "allowed"
else:
return "not allowed"
def enter_scrollbar():
"""This function checks for name and age."""
name = input("What is your name? ")
age = int(input("How old are you? "))
legal=check_age(age)
message = print(f"Welcome {name}. You are {legal} to drink beer.")
enter_scrollbar()
Upvotes: -1