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.
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
.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.