1

I am attempting to create a contact list with a def() function to easily loop back to the top later in the code. The issue I am having is that I define "function_question" in the def portion but when I run the code it gives me a NameError saying "function_question" is not defined. Any help or suggestions is appreciated!!

#Created a def so I can easily run back the code to the top. 
def user_prompt():
  print('Welcome to your contact directory.')
  function_question = int(input("What would you like to do? \n1  Add Conctact \n2  Find Contact \n3  Edit Contact \n4  Delete Contact:"))

user_prompt()

#Adding a contact to the contact list. 
while function_question == 1:
  name = input('What is the persons name? ')
  phone_number = input('What is the phone number? ')
  email = input('What is your email? ')
  address = input('What is the person adress? ')
  if len(phone_number) != 10:
    phone_number = input("the phone number you provided is not the proper length. Re-enter:")
  contact = [] + [name] + [phone_number] + [email] + [address]
  contact_list.append(contact)
  ans = input('Would you like to add another contact? ')
  if ans == 'yes':
    continue 
  if ans == 'no':
    user_prompt()
1
  • 1
    user_prompt creates a local name function_question which is deleted at the end of the function. In contrast, while function_question == 1: reads a global name function_question. Commented Feb 9, 2021 at 11:03

3 Answers 3

2

You could simply return the value from the function and save it to a variable outside the function. Like:

def user_prompt():
  print('Welcome to your contact directory.')
  return int(input("question"))

input_question = user_prompt()
Sign up to request clarification or add additional context in comments.

Comments

0

The issue is that the variable function_question is empty. In your code you defined two variables function_question with the same name but with different memory address; the first is a local variable that works only into the user_prompt() function. The correct code is:

#Created a def so I can easily run back the code to the top. 
def user_prompt():
  print('Welcome to your contact directory.')
  return int(input("What would you like to do? \n1  Add Conctact \n2  Find Contact \n3  Edit Contact \n4  Delete Contact:"))

function_question = user_prompt()

#Adding a contact to the contact list. 
while function_question == 1:
  ...

I suggest you to search about python scope variable for more details.

Comments

-1

The function_question variable is in the scope of the user_prompt function so you cannot use it in the global scope. You need to make it global for it to be acessible I reccomend reading through this, https://www.w3schools.com/PYTHON/python_scope.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.