gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:14:16 25 4
gpt4 key购买 nike

我正在使用 mysql-connector-python 创建一个大型程序,但在使用 if-else 后,它陷入了一个错误:变量行未定义 for 循环 中的语句。

这是我遇到错误的代码部分:

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 循环范围之外引用了变量 row

for row, col in mycursor.fetchall():
# notice that row and passw1 are now indented inside the for loop block
if email1 == row and passw1 == col:
print("Successfuly signed in!!")
break

参见python-scopes-and-namespaces了解更多详情。

关于python - Python变量行中的for循环问题未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com