未定义Python变量行中for循环的问题

未定义Python变量行中for循环的问题,python,python-3.x,for-loop,if-statement,Python,Python 3.x,For Loop,If Statement,我正在用mysql-connector-python创建一个大型程序,但它遇到了一个错误,即在for-loop中使用if-else语句后未定义变量行 下面是我的代码部分,其中有一个错误: elif question == 'login' or 'login'.upper(): email1 = input("Your email: ") passw1 = input("Your password: ") if '@' and '.' or '.co

我正在用
mysql-connector-python
创建一个大型程序,但它遇到了一个错误,即在
for-loop
中使用if-else语句后未定义变量行

下面是我的代码部分,其中有一个错误:

elif question == 'login' or 'login'.upper():
        email1 = input("Your email: ")
        passw1 = input("Your password: ")
        if '@' and '.' or '.com' in email:
            if len(passw1) > 5:
                mycursor.execute("USE register;")
                mycursor.execute("SELECT * FROM id;")
       # ------------------------------------------------#
                for row, col in mycursor.fetchall():    #|
                    pass                                #|    Here is the line which is affecting my code
                if email1 in row:                       #|   
                    if passw1 in col:                   #|
       # ------------------------------------------------#
                        print("Successfuly signed in!!")
                        break
import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="",
    database="register"
)

mycursor = mydb.cursor()
# OTP
OTP = 1234
# asking to choose
question = input("Would you like to login or signup?? ")
# sign up
while True:
    if question == 'signup':
        email = input("--------------------------\nYour email: ")
        passw = input("Your password: ")
        confirm_pass = input("Confirm password: ")
        mycursor.execute("USE register;")
        mycursor.execute("SELECT * FROM id;")
        for row, col in mycursor.fetchall():
            pass
        if email in row:
            if passw in col:
                print("Already logged in!!")
        elif '@' and '.' or '.com' in email:
            if passw == confirm_pass:
                if len(passw) > 5:
                    print("----------------------------\nSuccessfuly registered!")
                    mycursor.execute("USE register;")
                    query = "INSERT INTO id(email, passw) VALUES(%s,%s);"
                    args = (email, passw)
                    mycursor.execute(query, args)
                    mydb.commit()
                    break
                else:
                    print("Your password is too short")

            else:
                print("Passwords didn't match!")

        else:
            print("wrong email")

     # login condition
    elif question == 'login' or 'login'.upper():
        email1 = input("Your email: ")
        passw1 = input("Your password: ")
        if '@' and '.' or '.com' in email:
            if len(passw1) > 5:
                mycursor.execute("USE register;")
                mycursor.execute("SELECT * FROM id;")
                for row, col in mycursor.fetchall():
                    pass
                if email1 in row:
                    if passw1 in col:
                        print("Successfuly signed in!!")
                        break
                    else:
                        print("wrong email or password!!")
                        forgot_pass = input("Forgot password? \nYes/no ")
                        if forgot_pass == 'yes':
                            print("Yes")
                        elif forgot_pass == 'no':
                            print("No")
                        else:
                            print("Blah blah blah")
                else:
                    print("wrong email or password!!")
                    forgot_pass = input("Forgot password? \nYes/no ")
                    if forgot_pass == 'yes':
                        print("We have send an OTP at your email")
                        enter_OTP = int(input("Type OTP within 1 minute "))
                        if enter_OTP == OTP:
                            change_passw = input("Type new password ")
                        else:
                            print("Wrong OTP!")
                            break
                    elif forgot_pass == 'no':
                        print("No")
                    else:
                        print("Blah blah blah")
            else:
                print("Your password is too short")
        else:
            print("wrong email")
    else:
        print("Blah blah blah...")
下面是完整的代码:

elif question == 'login' or 'login'.upper():
        email1 = input("Your email: ")
        passw1 = input("Your password: ")
        if '@' and '.' or '.com' in email:
            if len(passw1) > 5:
                mycursor.execute("USE register;")
                mycursor.execute("SELECT * FROM id;")
       # ------------------------------------------------#
                for row, col in mycursor.fetchall():    #|
                    pass                                #|    Here is the line which is affecting my code
                if email1 in row:                       #|   
                    if passw1 in col:                   #|
       # ------------------------------------------------#
                        print("Successfuly signed in!!")
                        break
import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="",
    database="register"
)

mycursor = mydb.cursor()
# OTP
OTP = 1234
# asking to choose
question = input("Would you like to login or signup?? ")
# sign up
while True:
    if question == 'signup':
        email = input("--------------------------\nYour email: ")
        passw = input("Your password: ")
        confirm_pass = input("Confirm password: ")
        mycursor.execute("USE register;")
        mycursor.execute("SELECT * FROM id;")
        for row, col in mycursor.fetchall():
            pass
        if email in row:
            if passw in col:
                print("Already logged in!!")
        elif '@' and '.' or '.com' in email:
            if passw == confirm_pass:
                if len(passw) > 5:
                    print("----------------------------\nSuccessfuly registered!")
                    mycursor.execute("USE register;")
                    query = "INSERT INTO id(email, passw) VALUES(%s,%s);"
                    args = (email, passw)
                    mycursor.execute(query, args)
                    mydb.commit()
                    break
                else:
                    print("Your password is too short")

            else:
                print("Passwords didn't match!")

        else:
            print("wrong email")

     # login condition
    elif question == 'login' or 'login'.upper():
        email1 = input("Your email: ")
        passw1 = input("Your password: ")
        if '@' and '.' or '.com' in email:
            if len(passw1) > 5:
                mycursor.execute("USE register;")
                mycursor.execute("SELECT * FROM id;")
                for row, col in mycursor.fetchall():
                    pass
                if email1 in row:
                    if passw1 in col:
                        print("Successfuly signed in!!")
                        break
                    else:
                        print("wrong email or password!!")
                        forgot_pass = input("Forgot password? \nYes/no ")
                        if forgot_pass == 'yes':
                            print("Yes")
                        elif forgot_pass == 'no':
                            print("No")
                        else:
                            print("Blah blah blah")
                else:
                    print("wrong email or password!!")
                    forgot_pass = input("Forgot password? \nYes/no ")
                    if forgot_pass == 'yes':
                        print("We have send an OTP at your email")
                        enter_OTP = int(input("Type OTP within 1 minute "))
                        if enter_OTP == OTP:
                            change_passw = input("Type new password ")
                        else:
                            print("Wrong OTP!")
                            break
                    elif forgot_pass == 'no':
                        print("No")
                    else:
                        print("Blah blah blah")
            else:
                print("Your password is too short")
        else:
            print("wrong email")
    else:
        print("Blah blah blah...")

您引用了for循环范围之外的变量

对于mycursor.fetchall()中的行、列:
#请注意,行和passw1现在缩进到for循环块中
如果email1==行,passw1==列:
打印(“成功登录!!”)
打破

有关更多详细信息,请参阅。

上述代码的格式是否与您机器上的代码完全相似?缩进在python中非常重要,它定义了作用域。检查
循环,如果
我认为这与您的问题无关,请注意:
问题=='login'或'login'。upper():
不等同于“问题等于'login',或等于'login'。这相当于“问题等于‘login’,或者字符串‘login’的布尔值为True”。在布尔上下文中,所有非空字符串都为true,因此无论
问题
的值如何,conditional都将始终计算为true。虽然讨论范围的评论者说得对,但请记住for循环块没有自己的范围。对范围(10)内的x执行
是完全有效的:通过;打印(x)
。如果循环至少迭代了一次,
x
在循环结束后仍然存在。是否确定
mycursor.fetchall()
返回一些内容?现在它还表示未定义行。为什么?术语挑剔:For循环没有自己的范围。也许最好说“您引用了for循环块外部的变量
”。在任何情况下,我都同意在循环中移动
if
是个好主意。