Home > Enterprise >  Python: Database do while
Python: Database do while

Time:12-07

Good day everyone, I wonder if you can help me in my problem regarding Python Database (SQLite3). I wanted to make a do-while in my program. So every time I successfully inputted a data or after doing the job, instead of going back instantly to the main menu, I wanted to make a question "Do you want to continue? Y/N" if "Y" then it will go to main menu and if "N" it will terminate the program. Thank you in advanced!

import Databaseprofile
MENU_PROMPT = """--- Barangay Resident Record ---
Please choose one of these options:

[1] Add a new resident/profile.
[2] See all residents list.
[3] Exit.

Your selection: """

def menu():
    connection = Databaseprofile.connect()
    Databaseprofile.create_tables(connection)
    while (user_input := input(MENU_PROMPT)) != "3":
        if user_input == "1":
            print("---Add a Resident---")
            name = input("Enter full name: ")
            birth = input("Enter birthdate (YY-MM-DD): ")
            sex = input("Enter Sex: ")
            purok = int(input("Enter your Purok: "))
            house = int(input("Enter house number: "))
            number = int(input("Enter mobile number: "))

            Databaseprofile.add_profile(connection, name, birth, sex, purok, house, number)
            print("---Added successfully!---")
        elif user_input == "2":
            print("--------------------------------------------------------------------------------------------------")
            print("---Resident's List---")
            profile = Databaseprofile.get_all_profile(connection)

            for prof in profile:
                print(f"{prof[0]}, {prof[1]}, {prof[2]}, {prof[3]}, Purok {prof[4]}, House Number {prof[5]}, Mobile Number {prof[6]}")
            print("--------------------------------------------------------------------------------------------------")
            #
        else:
            print("Invalid input, try again.")

    print("Thank you for using our program.")
menu()

CodePudding user response:

find() will return the index of a substring, or -1 if the substring does not exist. The following will loop until standard input receives a "yes" or "no" response:

print("---Added successfully!---")
# loop until acceptable input is received
while True:
    response = input("Do you want to continue? Y/N")[0]
    if 'yYnN'.find(response) > -1:
        break

# exit outer loop and terminate the program
if 'nN'.find(response) > -1:
    break
  • Related