Reputation:
I am creating a large program with mysql-connector-python
but it gets stuck on one error that variable row is not defined after using if-else statement in for loop
.
Here is my portion of code where i am getting error:
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
And here is the full code:
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...")
Upvotes: 0
Views: 168
Reputation: 186
You reference a variable row
outside of the for loop scope.
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
See python-scopes-and-namespaces for more details.
Upvotes: 1