Python:数据库执行while



大家好,我想知道您是否可以帮助我解决有关Python数据库(SQLite3(的问题。我想在我的节目中凑合一下。因此,每次我成功输入数据或完成工作后,我都想提出一个问题,而不是立即返回主菜单;是否要继续?Y/N";如果";Y";则它将转到主菜单,并且如果"是";N〃;它将终止程序。提前感谢!

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()

find()将返回子字符串的索引,如果子字符串不存在,则返回-1。以下将循环直到标准输入接收到"0";是";或";否";响应:

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

最新更新