I have some code and I have assigned a variable as global but when trying to use the variable as validation in a separate function, it throws an exception.
This is the unfinished code (I know it won't work properly at the moment but I want to fix this first) for some school homework, I know there are probably much more efficient was to achieve my purpose but I want to understand why this isn't working.
def mainFunc():
nameList = []
print("1. Add name \n2. Display list \n3. Quit\n")
choice = displayMenu()
if choice == 1:
addName()
elif choice == 2:
displayList()
else:
print("Program Terminating")
def displayMenu():
global answer
answer = int(input("Please enter your choice: "))
answerCheck()
def answerCheck():
if answer == 1 or answer == 2 or answer == 3:
return(answer)
else:
answer = input("Invalid selection, please re-enter: ")
answerCheck()
def addName():
position = int(input("Please enter the position of the name: "))
name = input("Please enter the name you wish to add: ")
remove = nameList[position-1]
nameList.remove(remove)
nameList.add(name,position)
print(nameList)
mainFunc()