em4995
em4995

Reputation: 77

Conditional Statements and multiple functions

My task is to:

  1. Define a function check_age that takes age as a parameter.
  2. If the age is at least 18, you return a string allowed.
  3. If the age is anything else, you return a string not allowed.
  4. You define a function enter_scrollbar.
  5. You ask the user for his / her name "What is your name? ", and assign the input value to a variable.
  6. You ask the user for his / her age "How old are you? ", and assign the input value to a variable.
  7. You call the function check_age() and give it the age as a parameter (inside the parenthesis). The returned string of the the function is stored in a new variable (i.e. legal).
  8. You print a personalized message to the user about if he / she is allowed to drink. Your output should look similar to Welcome _____. You are __________ to drink beer.
  9. Call the function enter_scrollbar() to run your code.

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

Answers (3)

Irfan wani
Irfan wani

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

The Pilot Dude
The Pilot Dude

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

Amit
Amit

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

Related Questions