简体   繁体   English

使用函数时发生Python代码错误

[英]Python code error while using functions

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 . 子表达式"yes".lower()始终产生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. 您只能在第一个if分支中定义password变量,但是在两种情况下都尝试读取它。 So if teacher is not equal to "yes", password is not defined. 因此,如果teacher不等于“是”,则未定义password

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() . 代替.lower您可以在input()之前使用str.casefold() 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. 这应该更改用户输入的所有小写字母,而没有.lower()函数的情况下,代码的结果可以用小写字母编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM