I'm currently writing some code for a project of mine, though I have been stuck on this for a good bit and It's getting to me.
The idea is to have some code that will show the users previous input, ask them to validate it and see if what they typed is correct than proceed according to a 'yes' or 'no' answer provided by the user.
If they type 'yes' I want the code to continue and commit the data into a database, if they type 'no' I want the code to loop back so they can re-enter any values. The problem is I can't seem to get the code to loop back on itself so the user can input new values, It always just continues on, saving it to the database regardless or throwing a NameError claiming the variable is not defined.
Here's the code:
while True:
clear()
print("\nPLEASE INPUT THE REQUIRED DATA.\n")
IN_NAME = input('Name: ')
IN_EMAIL = input('Email: ')
IN_ADDRESS = input('Address: ')
clear()
print("\nDOES THIS LOOK CORRECT?\n")
print("#--START--#\n")
print("NAME: " IN_NAME)
print("EMAIL: " IN_EMAIL)
print("ADDRESS " IN_ADDRESS)
print("\n#---END---#\n")
loop_input = None
while True:
answer = input("ENTER YES OR NO: ")
if answer == "yes":
loop_repeat == False
break
elif answer == "no":
loop_repeat == True
break
else:
print("PLEASE ENTER YES OR NO.")
continue
if loop_repeat == True:
continue
else:
pass
cursor.execute("""
INSERT INTO contacts(name, email, address)
VALUES (?,?,?)
""", (IN_NAME, IN_EMAIL, IN_ADDRESS))
print ( 'Data entered successfully.\n' )
break
Any help would be greatly appreciated.
CodePudding user response:
simply change from this
while True:
answer = input("ENTER YES OR NO: ")
if answer == "yes":
loop_repeat == False
break
elif answer == "no":
loop_repeat == True
break
else:
print("PLEASE ENTER YES OR NO.")
continue
to this
while True:
answer = input("ENTER YES OR NO: ")
if answer == "yes":
loop_repeat = False
break
elif answer == "no":
loop_repeat = True
break
else:
print("PLEASE ENTER YES OR NO.")
continue
The problem was that you weren't doing an assignement.
== is used for comparisions, while you were using it for assignments (the latter is done using a single =
CodePudding user response:
You can do it function based
def ask_questions():
clear()
print("\nPLEASE INPUT THE REQUIRED DATA.\n")
IN_NAME = input('Name: ')
IN_EMAIL = input('Email: ')
IN_ADDRESS = input('Address: ')
clear()
print("\nDOES THIS LOOK CORRECT?\n")
print("#--START--#\n")
print("NAME: " IN_NAME)
print("EMAIL: " IN_EMAIL)
print("ADDRESS " IN_ADDRESS)
print("\n#---END---#\n")
while True:
answer = input("ENTER YES OR NO: ")
if answer == "yes":
return IN_NAME, IN_EMAIL, IN_ADDRESS
elif answer == "no":
return ask_questions()
else:
print("PLEASE ENTER YES OR NO.")
continue
name, email, address = ask_questions()
cursor.execute("""
INSERT INTO contacts(name, email, address)
VALUES (?,?,?)
""", (name, email, address))
print('Data entered successfully.\n')
