0

I have this code I want it to ask the question as many times as it needs to until a yes or no answer is given

def teacheraskno():
teacher = input("Are you a teacher? yes and no answers only! > ")
if teacher == "no" or "yes".lower():
 if teacher == "no".lower():
 start()
 if teacher == "yes".lower():
 teacheraskyes()
else:
 print ("Please enter yes and no answers only!")
 teacheraskno()
def teacheraskyes():
if teacher == "yes".lower(): 
 password = input("What is the Password? > ")
if password =="123".lower(): 
 print ("ACCESS GRANTED!")
 classname = input("what class would you like to view? 1, 2 or 3 > ")
 f = open(classname + ".txt", 'r') #opens the class file
 file_contents = f.read()
 print (file_contents)
 f.close()
teacher = input("Are you a teacher? yes and no answers only! > ")
if teacher == "no" or "yes".lower():
 if teacher == "no".lower():
 start()
 if teacher == "yes".lower():
 teacheraskyes()
 else:
 print ("Please enter yes and no answers only!")
 teacheraskno()

I keep getting this error

==============================Math Revision Quiz================================
Are you a teacher? yes and no answers only! > bla
Please enter yes and no answers only!
Are you a teacher? yes and no answers only! > yes
Traceback (most recent call last):
 File "S:\My Documents\Ben Atia CA A453\Python Code\Python Code 1.py", line 142, in <module>
 teacheraskno()
 File "S:\My Documents\Ben Atia CA A453\Python Code\Python Code 1.py", line 118, in teacheraskno
 teacheraskyes()
 File "S:\My Documents\Ben Atia CA A453\Python Code\Python Code 1.py", line 125, in teacheraskyes
 if password =="123".lower(): #if the password is correct it will let the teacher view the code
UnboundLocalError: local variable 'password' referenced before assignment
>>> 

What does this error mean and how can I fix it?
Please help me to solve this problem.

Akshay Hazari
3,2874 gold badges56 silver badges99 bronze badges
asked Dec 2, 2015 at 12:21
4
  • 1
    You should be using teacher.lower() instead of "yes".lower() or "no".lower(). "no" and "yes" are already lowercase... Also you should compare like this teacher.lower() == "yes" or teacher.lower() == "no" Commented Dec 2, 2015 at 12:25
  • Please don't use 123 as password and please don't hard-code it! :-/ Commented Dec 2, 2015 at 12:30
  • Please format your code and traceback. Commented Dec 2, 2015 at 12:38
  • 1
    In Python indentation is part of language syntax. Please, fix it so whitespace would actually denote code blocks. Commented Dec 2, 2015 at 12:53

3 Answers 3

2

You should change this line

if teacher == "no" or "yes".lower():

To

if teacher.lower() not in ("no", "yes"):

As currently written, the expression doesn't mean what you think it does. If I add parentheses for emphasis, your expression actually reads as

if (teacher == "no") or ("yes".lower()):

The subexpression "yes".lower() will always yield True.

answered Dec 2, 2015 at 12:29
Sign up to request clarification or add additional context in comments.

Comments

0

It's hard to tell for sure given your snippet's boken indentation, but here:

if teacher == "yes".lower(): 
 password = input("What is the Password? > ")
if password =="123".lower(): 
 print ("ACCESS GRANTED!")

you only define the password variable in the first if branch, but try to read it in both cases. So if teacher is not equal to "yes", password is not defined.

There are quite a few other problems with your code, but that's outside the scope of your question.

answered Dec 2, 2015 at 12:43

Comments

0

Instead of .lower you could use str.casefold() before input(). This will make anything the user enters into lower case. Not only is this a validation but it allows you to write all your code in lowercase. For example:

teacher = str.casefold(input("Are you a teacher?"))

This should change anything the user enters lowercase and the res of your code could be written in lowercase without the .lower() function.

answered Feb 1, 2017 at 18:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.